import { u64, i64, toU64, toI64, Registers, SegmentRegisters } from '../types';
import { RegisterManager } from '../cpu/Registers';
import { MemoryManager } from '../memory/MemoryManager';
import { InstructionDecoder, DecodedInstruction, PrefixType } from '../decoder/InstructionDecoder';
import { MachineMode, u16 } from '../types';

// XMM寄存器大小（128位 = 16字节）
const XMM_REGISTER_SIZE = 16;

export class InstructionExecutor {
  private registerManager: RegisterManager;
  private memoryManager: MemoryManager;
  private instructionDecoder: InstructionDecoder;
  private mode: MachineMode; // 机器模式

  constructor(
    registerManager: RegisterManager,
    memoryManager: MemoryManager,
    instructionDecoder: InstructionDecoder,
    mode: MachineMode = MachineMode.LONG
  ) {
    this.registerManager = registerManager;
    this.memoryManager = memoryManager;
    this.instructionDecoder = instructionDecoder;
    this.mode = mode;
  }

  // 执行单条指令
  executeInstruction(instruction: DecodedInstruction): void {
    const opcode = instruction.opcode;

    // 处理8位ALU翻转操作指令 (OpAlubFlip)
    // 这些指令是8位ALU操作的翻转形式，操作数顺序相反
    if (opcode >= 0x10 && opcode <= 0x17) {
      this.executeAlubFlip(instruction, opcode - 0x10);
      return;
    }

    // 处理16位ALU翻转操作指令 (OpAluwFlip)
    // 这些指令是16位ALU操作的翻转形式，操作数顺序相反
    // 需要检查前缀来确定是16位操作
    if (instruction.prefixes.has(PrefixType.OPERAND_SIZE) && opcode >= 0x10 && opcode <= 0x17) {
      this.executeAluwFlip(instruction, opcode - 0x10);
      return;
    }

    // 处理32位ALU翻转操作指令 (OpAludFlip)
    // 在64位模式下，这些指令是32位ALU操作的翻转形式
    if (this.mode === MachineMode.LONG && opcode >= 0x10 && opcode <= 0x17) {
      this.executeAludFlip(instruction, opcode - 0x10);
      return;
    }

    // 处理AL寄存器与立即数的ALU操作 (OpAluAlIb系列)
    if (opcode >= 0x04 && opcode <= 0x07) {
      this.executeAluAlIb(instruction, opcode - 0x04);
      return;
    }
    if (opcode >= 0x80 && opcode <= 0x83) {
      // 检查modrm是否指示是AL寄存器与立即数操作
      if (instruction.modrm && instruction.modrm.reg === 0) {
        this.executeAluAlIbExtended(instruction, opcode - 0x80);
        return;
      }
    }
    // 处理AL寄存器与立即数的AND和XOR操作
    if (opcode === 0x24 || opcode === 0x34) {
      this.executeAluAlIbAndXor(instruction, opcode);
      return;
    }
    // 处理AL寄存器与立即数的减法操作 (OpAluAlIbSub)
    if (opcode === 0x2C) {
      this.executeAluAlIbSub(instruction);
      return;
    }

    // 处理系统指令
    // 检查0x0F操作码
    if (opcode === 0x0F) {
      const extendedOpcode = instruction.immediate || 0n;
      switch (Number(extendedOpcode)) {
        case 0x31: // RDTSC (OpRdtsc) - 读取时间戳计数器
          this.executeRdtsc();
          return;
        case 0x32: // RDMSR (OpRdmsr) - 读取模型特定寄存器
          this.executeRdmsr();
          return;
      }
    }

    // 处理syscall指令 (OpSyscall)
    if (opcode === 0x0F && instruction.immediate === 0x05n && this.mode === MachineMode.LONG) {
      this.executeSyscall();
      return;
    }
    // 处理AL寄存器与立即数的比较和测试操作
    if (opcode === 0x3C || opcode === 0xA8) {
      this.executeAluAlIbCompareTest(instruction, opcode);
      return;
    }

    // 根据操作码执行相应的指令
    switch (opcode) {
      case 0x90: // NOP 或 PAUSE (带0xF3前缀)
        if (instruction.prefixes && instruction.prefixes.has(0xF3)) {
          this.executePause();
        } else {
          this.executeNop();
        }
        break;

      case 0x9A: // CALLF - 远调用指令
        this.executeCallf(instruction);
        break;

      case 0x50: // PUSH RAX
      case 0x51: // PUSH RCX
      case 0x52: // PUSH RDX
      case 0x53: // PUSH RBX
      case 0x54: // PUSH RSP
      case 0x55: // PUSH RBP
      case 0x56: // PUSH RSI
      case 0x57: // PUSH RDI
        this.executePushRegister(opcode - 0x50);
        break;
      case 0x58: // POP RAX
      case 0x59: // POP RCX
      case 0x5A: // POP RDX
      case 0x5B: // POP RBX
      case 0x5C: // POP RSP
      case 0x5D: // POP RBP
      case 0x5E: // POP RSI
      case 0x5F: // POP RDI
        this.executePopRegister(opcode - 0x58);
        break;
      case 0xB8: // MOV RAX, imm64
      case 0xB9: // MOV RCX, imm64
      case 0xBA: // MOV RDX, imm64
      case 0xBB: // MOV RBX, imm64
      case 0xBC: // MOV RSP, imm64
      case 0xBD: // MOV RBP, imm64
      case 0xBE: // MOV RSI, imm64
      case 0xBF: // MOV RDI, imm64
        this.executeMovRegisterImmediate(opcode - 0xB8, instruction.immediate!);
        break;
      case 0x89: // MOV r/m64, r64 或 MOV r/m32, r32 或 MOV r/m8, r8
        // 根据操作数大小前缀确定操作数大小
        if (instruction.prefixes.has(PrefixType.OPERAND_SIZE)) {
          this.executeMovl(instruction);
        } else {
          // 检查是否是8位操作（通过ModR/M的reg字段）
          if (instruction.modrm && instruction.opcode === 0x88) {
            this.executeMovb(instruction);
          } else {
            this.executeMovRmToR(instruction);
          }
        }
        break;
      case 0x8B: // MOV r64, r/m64 或 MOV r32, r/m32 或 MOV r8, r/m8 或 MOV r16, r/m16
        // 根据操作数大小前缀确定操作数大小
        if (instruction.prefixes.has(PrefixType.OPERAND_SIZE)) {
          this.executeMovl(instruction);
        } else {
          // 检查是否是8位操作（通过ModR/M的reg字段）
          if (instruction.modrm && instruction.opcode === 0x8A) {
            this.executeMovb(instruction);
          } else {
            this.executeMovRToRm(instruction);
          }
        }
        break;
      case 0x86: // XCHG r/m8, r8
        this.executeXchgRmR(instruction);
        break;
      case 0x87: // XCHG r/m64, r64 或 XCHG r/m16, r16 或 XCHG r/m32, r32
        this.executeXchgRmR(instruction);
        break;
      case 0x88: // MOV r/m8, r8
        this.executeMovb(instruction);
        break;
      case 0x8A: // MOV r8, r/m8
        this.executeMovb(instruction);
        break;
      case 0xC6: // MOV r/m8, imm8
        if (instruction.modrm && instruction.modrm.reg === 0) {
          this.executeMovb(instruction);
        }
        break;
      case 0x01: // ADD r/m64, r64
        this.executeAddRmR(instruction);
        break;
      case 0x03: // ADD r64, r/m64
        this.executeAddRRm(instruction);
        break;
      case 0x29: // SUB r/m64, r64
        this.executeSubRmR(instruction);
        break;
      case 0x2B: // SUB r64, r/m64
        this.executeSubRRm(instruction);
        break;
      case 0x39: // CMP r/m64, r64
        this.executeCmpRmR(instruction);
        break;
      case 0x3B: // CMP r64, r/m64
        this.executeCmpRRm(instruction);
        break;
      case 0x20: // AND r/m64, r64
        this.executeAndRmR(instruction);
        break;
      case 0x23: // AND r64, r/m64
        this.executeAndRRm(instruction);
        break;
      case 0x08: // OR r/m64, r64
        this.executeOrRmR(instruction);
        break;
      case 0x0B: // OR r64, r/m64
        this.executeOrRRm(instruction);
        break;
      case 0x30: // XOR r/m64, r64
        this.executeXorRmR(instruction);
        break;
      case 0x33: // XOR r64, r/m64
        this.executeXorRRm(instruction);
        break;
      case 0xF7: // NEG r/m64 或 NOT r/m64 (根据ModR/M的reg字段确定)
        if (instruction.modrm) {
          switch (instruction.modrm.reg) {
            case 2: // NOT r/m64
              this.executeNot(instruction);
              break;
            case 3: // NEG r/m64
              this.executeNeg(instruction);
              break;
          }
        }
        break;
      case 0xE4: // IN AL, imm8 (从立即数指定的端口读取8位数据到AL寄存器)
        this.executeInImm8Al(instruction);
        break;
      case 0xE5: // IN AX, imm8 (从立即数指定的端口读取16位数据到AX寄存器)
        this.executeInImm8Ax(instruction);
        break;
      case 0xEC: // IN AL, DX (从DX寄存器指定的端口读取8位数据到AL寄存器)
        this.executeInDxAl(instruction);
        break;
      case 0xED: // IN AX, DX (从DX寄存器指定的端口读取16位数据到AX寄存器) 或 IN EAX, DX (64位模式下为32位数据)
        // 假设在64位模式下运行
        this.executeInDxEax(instruction);
        break;
      case 0xE6: // OUT imm8, AL (从AL寄存器输出8位数据到立即数指定的端口)
        this.executeOutImm8Al(instruction);
        break;
      case 0xE7: // OUT imm8, AX (从AX寄存器输出16位数据到立即数指定的端口)
        this.executeOutImm8Ax(instruction);
        break;
      case 0xEE: // OUT DX, AL (从AL寄存器输出8位数据到DX寄存器指定的端口)
        this.executeOutDxAl(instruction);
        break;
      case 0xEF: // OUT DX, AX (从AX寄存器输出16位数据到DX寄存器指定的端口) 或 OUT DX, EAX (64位模式下为32位数据)
        // 假设在64位模式下运行
        this.executeOutDxEax(instruction);
        break;
      case 0x0FBD: // BSF r64, r/m64 - 位扫描正向（寻找最低位的1）
        this.executeBsf(instruction);
        break;
      case 0x0FBC: // BSR r64, r/m64 - 位扫描反向（寻找最高位的1）
        this.executeBsr(instruction);
        break;
      case 0x0F58: // ADDPD/ADDPS - 双精度/单精度浮点数加法
        this.executeAddpdAddps(instruction);
        break;
      case 0x0F5E: // DIVPD/DIVPS - 双精度/单精度浮点数除法
        // 没有66H前缀是DIVPS（单精度），有66H前缀是DIVPD（双精度）
        const isDoublePrecisionDiv = instruction.prefixes.has(PrefixType.OPERAND_SIZE);
        if (isDoublePrecisionDiv) {
          this.executeDivpd(instruction);
        } else {
          this.executeDivps(instruction);
        }
        break;
      case 0x0F54: // ANDPS - 单精度浮点数按位与
        this.executeAndps(instruction);
        break;
      case 0x0F5F: // MAXPS - 单精度浮点数最大值
        this.executeMaxps(instruction);
        break;
      case 0x0F5D: // MINPS - 单精度浮点数最小值
        this.executeMinps(instruction);
        break;
      case 0x0F28: // MOVAPD - 对齐双精度浮点数移动
        this.executeMovapd(instruction);
        break;
      case 0x0F2B: // MOVSD - 双精度浮点数移动
        this.executeMovsd(instruction);
        break;
      case 0x0F11: // MOVSS - 单精度浮点数移动 (XMM寄存器到内存)
      case 0x0F10: // MOVSS - 单精度浮点数移动 (内存到XMM寄存器)
        this.executeMovss(instruction);
        break;
      case 0x0F12: // MOVUPS - 非对齐单精度浮点数移动 (内存到XMM寄存器)
      case 0x0F16: // MOVUPS - 非对齐单精度浮点数移动 (XMM寄存器到内存)
        this.executeMovups(instruction);
        break;
      case 0x0F59: // MULPD/MULPS - 双精度/单精度浮点数乘法
        // 没有66H前缀是MULPS（单精度），有66H前缀是MULPD（双精度）
        const isDoublePrecisionMul = instruction.prefixes.has(PrefixType.OPERAND_SIZE);
        if (isDoublePrecisionMul) {
          this.executeMulpd(instruction);
        } else {
          this.executeMulps(instruction);
        }
        break;
      case 0x0F56: // ORPS - 单精度浮点数按位或
        this.executeOrps(instruction);
        break;
      case 0x0F50: // MOVMSKPS - 单精度浮点数掩码移动
        this.executeMovmskps(instruction);
        break;
      case 0x0F38: // MOVNTDQA - 非临时对齐双字加载
        if (instruction.modrm && instruction.modrm.reg === 4) {
          this.executeMovntdqa(instruction);
        }
        break;
      case 0x0F2B: // MOVNTI - 非临时四字存储
        this.executeMovnti(instruction);
        break;
      case 0x0F6F: // MOVQ - 四字移动 (XMM寄存器到XMM寄存器或内存)
        this.executeMovq(instruction);
        break;
      case 0x0F2F: // CMPPS/CMPPD - 单精度/双精度浮点数比较
        // 检查是否是COMISS指令（通过前缀区分）
        if (instruction.prefixes.has(0xF3)) {
          this.executeComiss(instruction);
        } else {
          this.executeCmppsCmppd(instruction);
        }
        break;
      case 0x0F73: // PABSB/PABSW/PABSD - 计算打包数据的绝对值
        if (instruction.modrm && instruction.modrm.reg === 1) {
          // 0F 73 /1 PABSB
          this.executePabsb(instruction);
        } else if (instruction.modrm && instruction.modrm.reg === 2) {
          // 0F 73 /2 PABSW
          this.executePabsw(instruction);
        } else if (instruction.modrm && instruction.modrm.reg === 3) {
          // 0F 73 /3 PABSD
          this.executePabsd(instruction);
        }
        break;
      case 0x660F63: // PACKSSDW - 打包带符号饱和的双字到字
        this.executePackssdw(instruction);
        break;
      case 0x660F6B: // PACKSSWB - 打包带符号饱和的字到字节
        this.executePacksswb(instruction);
        break;
      case 0x0F6B: // PACKUSWB - 打包无符号饱和的字到字节
        this.executePackuswb(instruction);
        break;
      case 0x660F3A0F: // PALIGNR - 对齐向量元素
        this.executePalignr(instruction);
        break;
      case 0x0F3A44: // PCLMULQDQ - 多项式乘法
        this.executePclmulqdq(instruction);
        break;
      case 0x0F74: // PCMPEQB - 打包字节比较
        this.executePcmpeqb(instruction);
        break;
      case 0x0F75: // PCMPEQW - 打包字比较
        this.executePcmpeqw(instruction);
        break;
      case 0x0F76: // PCMPEQD - 打包双字比较
        this.executePcmpeqd(instruction);
        break;
      case 0x0F66: // PCMPGTB - 打包字节大于比较
        this.executePcmpgtb(instruction);
        break;
      case 0x0F67: // PCMPGTW - 打包字大于比较
        this.executePcmpgtw(instruction);
        break;
      case 0x0F68: // PCMPGTD - 打包双字大于比较
        this.executePcmpgtd(instruction);
        break;
      case 0x0F60: // PUNPCKHBW - 高位字节解包
        this.executePunpckhbw(instruction);
        break;
      case 0x0F61: // PUNPCKHDQ - 高位双字解包
        this.executePunpckhdq(instruction);
        break;
      case 0x0F62: // PUNPCKHQDQ - 高位四字解包
        this.executePunpckhqdq(instruction);
        break;
      case 0x0F63: // PUNPCKHWD - 高位字解包
        this.executePunpckhwd(instruction);
        break;
      case 0x0F64: // PUNPCKLBW - 低位字节解包
        this.executePunpcklbw(instruction);
        break;
      case 0x0F65: // PUNPCKLDQ - 低位双字解包
        this.executePunpckldq(instruction);
        break;
      case 0x0F69: // PUNPCKLQDQ - 低位四字解包
        this.executePunpcklqdq(instruction);
        break;
      case 0x0F6A: // PUNPCKLWD - 低位字解包
        this.executePunpcklwd(instruction);
        break;
      case 0x60: // PUSHA - 压入所有通用寄存器
        this.executePusha(instruction);
        break;
      case 0x0F53: // RCPPS - 计算单精度浮点数的倒数
        this.executeRcpps(instruction);
        break;
      case 0x0F17: // UNPCKHPS - 高位单精度浮点数解包
        this.executeUnpckhps(instruction);
        break;
      case 0x0F14: // UNPCKLPS - 低位单精度浮点数解包
        this.executeUnpcklps(instruction);
        break;
      case 0x0F2E: // UCOMISS - 单精度浮点数无符号比较
        this.executeUcomiss(instruction);
        break;
      case 0x660F57: // XORPSD - 双精度浮点数按位异或
        this.executeXorpd(instruction);
        break;
      case 0x0F57: // XORPS - 单精度浮点数按位异或
        this.executeXorps(instruction);
        break;
      case 0x0F30: // WRMSR - 写入模型特定寄存器
        this.executeWrmsr();
        break;
      case 0x0F3A16: // PEXTRW - 提取字
        this.executePextrw(instruction);
        break;
      case 0x0F3A41: // PINSRW - 插入字
        this.executePinsrw(instruction);
        break;
      case 0x660F3804: // PMULLD - 打包双字相乘
        this.executePmulld(instruction);
        break;
      case 0x61: // POPA - 弹出所有通用寄存器
        this.executePopa(instruction);
        break;
      case 0x0FA3: // BT r/m64, r64 - 位测试
      case 0x0FB3: // BTC r/m64, r64 - 位测试并取反
      case 0x0FAB: // BTR r/m64, r64 - 位测试并复位
      case 0x0FAD: // BTS r/m64, r64 - 位测试并置位
      case 0xBA: // BT r/m64, imm8 - 位测试带立即数
        this.executeBitOp(instruction);
        break;
      case 0x0F01: // XADD r/m64, r64 - 交换并相加 或 XGETBV/XSETBV (通过ModR/M的reg字段区分)
        if (instruction.modrm) {
          // 检查是否是XGETBV/XSETBV指令 (在64位模式下，这些指令使用特殊的ModR/M编码)
          if (instruction.modrm.mod === 3 && instruction.modrm.rm === 0) {
            switch (instruction.modrm.reg) {
              case 0: // XGETBV - 读取扩展控制寄存器
                this.executeXgetbv();
                break;
              case 1: // XSETBV - 设置扩展控制寄存器
                this.executeXsetbv();
                break;
              default:
                this.executeXadd(instruction);
                break;
            }
          } else {
            this.executeXadd(instruction);
          }
        }
        break;
      case 0x0F33: // RDPMC - 读取性能监控计数器
        this.executeRdpmc();
        break;
      case 0xF4: // HLT - 停机指令
        this.executeHlt();
        break;
      case 0x9B: // FWAIT/WAIT - 等待指令
        this.executeFwait();
        break;
      case 0x0F77: // EMMS - 清空多媒体状态指令
        this.executeEmms();
        break;
      case 0x0FF0: // LDDQU - 非对齐双字加载
        this.executeLddqu(instruction);
        break;
      case 0x0FF1: // PSLLWV - 可变宽度左移位（字）
        this.executePsllwv(instruction);
        break;
      case 0x0FF2: // PSLLDV - 可变宽度左移位（双字）
        this.executePslldv(instruction);
        break;
      case 0x0FF3: // PSLLQV - 可变宽度左移位（四字）
        this.executePsllqv(instruction);
        break;
      case 0x0FEC: // PADDSB - 带符号饱和字节加法
        this.executePaddsb(instruction);
        break;
      case 0x0FED: // PADDSW - 带符号饱和字加法
        this.executePaddsw(instruction);
        break;
      case 0x0FEF: // PMULUDQ - 无符号双字乘法
        this.executePmuludq(instruction);
        break;
      case 0x0FF5: // PMADDWD - 字乘法加法
        this.executePmaddwd(instruction);
        break;
      case 0x0FF6: // PSADBW - 绝对差求和
        this.executePsadbw(instruction);
        break;
      case 0x0F1F7: // MASKMOVQ/MASKMOVDQU - 掩码移动
        this.executeMaskMovDiXmmRegXmmRm(instruction);
        break;
      case 0x0FEE: // PMAXSW - 带符号字最大值
        this.executePmaxsw(instruction);
        break;
      case 0x0F1EA: // PMINSW - 带符号字最小值
        this.executePminsw(instruction);
        break;
      case 0x0F1F8: // PSUBB - 字节减法
        this.executePsubb(instruction);
        break;
      case 0x0F1F9: // PSUBW - 字减法
        this.executePsubw(instruction);
        break;
      case 0x0F1FA: // PSUBD - 双字减法
        this.executePsubd(instruction);
        break;
      case 0x0F1FB: // PSUBQ - 四字减法
        this.executePsubq(instruction);
        break;
      case 0x0F1FC: // PADDB - 字节加法
        this.executePaddb(instruction);
        break;
      case 0x0F1FD: // PADDW - 字加法
        this.executePaddw(instruction);
        break;
      case 0x0F1FE: // PADDD - 双字加法
        this.executePaddd(instruction);
        break;
      case 0x0F200: // PSHUFB - 字节洗牌
        this.executePshufb(instruction);
        break;
      case 0x0F201: // PHADDW - 水平字加法
        this.executePhaddw(instruction);
        break;
      case 0x0F202: // PHADDD - 水平双字加法
        this.executePhaddd(instruction);
        break;
      case 0x0F203: // PHADDSW - 水平带符号饱和字加法
        this.executePhaddsw(instruction);
        break;
      case 0x0F204: // PMADDUBSW - 无符号字节乘法带符号字加法
        this.executePmaddubsw(instruction);
        break;
      case 0x0F205: // PHSUBW - 水平字减法
        this.executePhsubw(instruction);
        break;
      case 0x0F206: // PHSUBD - 水平双字减法
        this.executePhsubd(instruction);
        break;
      case 0x0F207: // PHSUBSW - 水平带符号饱和字减法
        this.executePhsubsw(instruction);
        break;
      case 0x0FB1: // CMPXCHG r/m64, r64 - 比较并交换
        this.executeCmpxchg(instruction);
        break;
      case 0x27: // DAA - 加法后的十进制调整
        this.executeDaa();
        break;
      case 0x2F: // DAS - 减法后的十进制调整
        this.executeDas();
        break;
      case 0x9C: // PUSHF/PUSHFD/PUSHFQ - 压入标志寄存器
        this.executePushf(instruction);
        break;
      case 0x9D: // POPF/POPFD/POPFQ - 弹出标志寄存器
        this.executePopf(instruction);
        break;
      case 0x9E: // SAHF - 将AH寄存器存储到标志寄存器低字节
        this.executeSahf(instruction);
        break;
      case 0x9F: // LAHF - 将标志寄存器低字节加载到AH寄存器
        this.executeLahf(instruction);
        break;
      case 0xC1: // 移位指令带立即数 (需要检查ModR/M的reg字段)
        if (instruction.modrm) {
          switch (instruction.modrm.reg) {
            case 0: // ROL r/m64, imm8
              this.executeRolImmediate(instruction);
              break;
            case 1: // ROR r/m64, imm8
              this.executeRorImmediate(instruction);
              break;
            case 2: // RCL r/m64, imm8
              this.executeRclImmediate(instruction);
              break;
            case 3: // RCR r/m64, imm8
              this.executeRcrImmediate(instruction);
              break;
            case 4: // SHL r/m64, imm8
              this.executeShlImm(instruction);
              break;
            case 5: // SHR r/m64, imm8
              this.executeShrImm(instruction);
              break;
            case 7: // SAR r/m64, imm8
              this.executeSarImm(instruction);
              break;
          }
        }
        break;
      case 0xC4: // LDS r16/r32/r64, m16:16/m16:32/m16:64
        this.executeLds(instruction);
        break;
      case 0xC5: // LES r16/r32/r64, m16:16/m16:32/m16:64
        this.executeLes(instruction);
        break;
      case 0xD3: // 移位指令带CL寄存器 (需要检查ModR/M的reg字段)
        if (instruction.modrm) {
          switch (instruction.modrm.reg) {
            case 0: // ROL r/m64, CL
              this.executeRolCl(instruction);
              break;
            case 1: // ROR r/m64, CL
              this.executeRorCl(instruction);
              break;
            case 2: // RCL r/m64, CL
              this.executeRclCl(instruction);
              break;
            case 3: // RCR r/m64, CL
              this.executeRcrCl(instruction);
              break;
            case 4: // SHL r/m64, CL
              this.executeShlCl(instruction);
              break;
            case 5: // SHR r/m64, CL
              this.executeShrCl(instruction);
              break;
            case 7: // SAR r/m64, CL
              this.executeSarCl(instruction);
              break;
          }
        }
        break;
      case 0xE8: // CALL rel32
        this.executeCallRelative(instruction.immediate!);
        break;
      case 0xFF: // CALL r/m64 (寄存器或内存间接调用) 或其他指令(需要检查ModR/M的reg字段)
        if (instruction.modrm) {
          switch (instruction.modrm.reg) {
            case 2: // CALL r/m64
              this.executeCallRM(instruction);
              break;
          }
        }
        break;
      case 0xC3: // RET
        this.executeRet();
        break;
      case 0xC2: // RET imm16 (带立即数返回，调整栈指针)
        this.executeRetImm(instruction.immediate!);
        break;
      case 0xC8: // ENTER imm16, imm8 (为过程创建栈帧)
        if (instruction.immediate !== undefined) {
          const localVarSize = Number(instruction.immediate & 0xFFFFn); // 低16位是局部变量大小
          const nestingLevel = Number((instruction.immediate >> 16n) & 0xFFn); // 接下来的8位是嵌套级别
          this.executeEnter(localVarSize, nestingLevel);
        }
        break;
      case 0xC9: // LEAVE (释放过程栈帧)
        this.executeLeave();
        break;
      case 0xCD: // INT imm8 (中断指令，带立即数)
        this.executeInt(instruction);
        break;
      case 0xCC: // INT3 (断点中断)
        this.executeInt3();
        break;
      case 0xCE: // INTO (溢出时中断)
        this.executeInto();
        break;
      case 0xCF: // IRET/IRETD/IRETQ (中断返回指令)
        this.executeIret();
        break;
      case 0xE9: // JMP rel32
        this.executeJmpRelative(instruction.immediate!);
        break;
      case 0x74: // JZ rel8
        this.executeJz(instruction.immediate!);
        break;
      case 0xF8: // CLC - 清除进位标志
        this.executeClc(instruction);
        break;
      case 0xF9: // STC - 设置进位标志
        this.executeStc(instruction);
        break;
      case 0xF5: // CMC - 翻转进位标志
        this.executeCmc(instruction);
        break;
      case 0xFA: // CLI - 清除中断标志
        this.executeCli(instruction);
        break;
      case 0xFB: // STI - 设置中断标志
        this.executeSti(instruction);
        break;
      case 0xFC: // CLD - 清除方向标志
        this.executeCld(instruction);
        break;
      case 0xFD: // STD - 设置方向标志
        this.executeStd(instruction);
        break;
      case 0x75: // JNZ/JNE rel8
        this.executeJnz(instruction.immediate!);
        break;
      case 0x7E: // JLE rel8
        this.executeJle(instruction);
        break;
      case 0x7F: // JG rel8
        this.executeJg(instruction);
        break;
      case 0x7D: // JGE rel8
        this.executeJge(instruction);
        break;
      case 0x7C: // JL rel8
        this.executeJl(instruction);
        break;
      case 0x71: // JNO rel8
        this.executeJno(instruction.immediate!);
        break;
      case 0x72: // JB/JC rel8
        this.executeJb(instruction.immediate!);
        break;
      case 0x73: // JAE/JNC rel8
        this.executeJae(instruction.immediate!);
        break;
      case 0x76: // JBE rel8
        this.executeJbe(instruction);
        break;
      case 0x77: // JA rel8
        this.executeJa(instruction);
        break;
      case 0x70: // JO rel8
        this.executeJo(instruction.immediate!);
        break;
      case 0x71: // JNO rel8
        this.executeJno(instruction.immediate!);
        break;
      case 0x72: // JB/JC rel8
        this.executeJb(instruction.immediate!);
        break;
      case 0x73: // JAE/JNC rel8
        this.executeJae(instruction.immediate!);
        break;
      case 0x74: // JZ rel8
        this.executeJz(instruction.immediate!);
        break;
      case 0x75: // JNZ/JNE rel8
        this.executeJnz(instruction.immediate!);
        break;
      case 0x78: // JS rel8
        this.executeJs(instruction.immediate!);
        break;
      case 0x79: // JNS rel8
        this.executeJns(instruction.immediate!);
        break;
      case 0x7A: // JP/JPE rel8
        this.executeJp(instruction.immediate!);
        break;
      case 0x7B: // JNP/JPO rel8
        this.executeJnp(instruction.immediate!);
        break;
      case 0xEB: // JMP rel8
        this.executeJmpRelative(instruction.immediate!);
        break;
      case 0xE2: // LOOP rel8
        this.executeLoop(instruction);
        break;
      case 0xE1: // LOOPE/LOOPZ rel8
        this.executeLoope(instruction);
        break;
      case 0xE0: // LOOPNE/LOOPNZ rel8
        this.executeLoopne(instruction);
        break;
      case 0xE3: // JCXZ/JECXZ/JRCXZ - CX/ECX/RCX为零则跳转
        this.executeJcxz(instruction);
        break;
      case 0x86: // XCHG r/m64, reg64
        this.executeXchgRmR(instruction);
        break;
      case 0x87: // XCHG reg64, r/m64
        this.executeXchgRRm(instruction);
        break;
      case 0xF7: // 乘法、除法和测试指令 (需要检查ModR/M的reg字段)
        if (instruction.modrm) {
          switch (instruction.modrm.reg) {
            case 0: // TEST r/m64, imm32
              this.executeTestImm(instruction);
              break;
            case 4: // MUL r/m64 - 无符号乘法
              this.executeMulRm(instruction);
              break;
            case 5: // IMUL r/m64 - 有符号乘法（单操作数形式）
              this.executeImulRm(instruction);
              break;
            case 6: // DIV r/m64 - 无符号除法
              this.executeDivRm(instruction);
              break;
            case 7: // IDIV r/m64 - 有符号除法
              this.executeIdivRm(instruction);
              break;
          }
        }
        break;
      case 0xA4: // MOVSB - 字节串移动
        this.executeMovsb(instruction);
        break;
      case 0xA5: // MOVS - 字串/双字串/四字串移动
        this.executeMovs(instruction);
        break;
      case 0xAA: // STOSB - 存储字节串
        this.executeStosb(instruction);
        break;
      case 0xAB: // STOS - 存储字串/双字串/四字串
        this.executeStos(instruction);
        break;
      case 0xAC: // LODSB - 加载字节串
        this.executeLodsb(instruction);
        break;
      case 0xAD: // LODS - 加载字串/双字串/四字串
        this.executeLods(instruction);
        break;
      case 0xA6: // CMPSB - 字节串比较
        this.executeCmpsb(instruction);
        break;
      case 0xA7: // CMPS - 字串/双字串/四字串比较
        this.executeCmps(instruction);
        break;
      case 0xAE: // SCASB - 字节串扫描
        this.executeScasb(instruction);
        break;
      case 0xAF: // SCAS - 字串/双字串/四字串扫描
        this.executeScas(instruction);
        break;
      case 0x0F05: // LFS r16/r32/r64, m16:16/m16:32/m16:64
        this.executeLfs(instruction);
        break;
      case 0x0F06: // LGS r16/r32/r64, m16:16/m16:32/m16:64 或 CLTS
        // 检查是否有ModR/M字节来区分LGS和CLTS指令
        if (instruction.modrm) {
          // 有ModR/M字节，是LGS指令
          this.executeLgs(instruction);
        } else {
          // 没有ModR/M字节，是CLTS指令（特权指令）
          this.executeClts();
        }
        break;
      case 0x0F08: // LSS r16/r32/r64, m16:16/m16:32/m16:64
        this.executeLss(instruction);
        break;
      case 0x0F09: // WBINVD - 使高速缓存无效并回写
        this.executeWbinvd();
        break;
      case 0x6C: // INSB - 从端口输入字节到内存
        this.executeIns(instruction);
        break;
      case 0x6D: // INSW/INSD - 从端口输入字/双字到内存
        this.executeIns(instruction);
        break;
      case 0x0F01: // 处理多种指令，包括XGETBV/XSETBV、MWAIT和INVLPG
        if (instruction.modrm) {
          // 检查是否是XGETBV/XSETBV指令 (在64位模式下，这些指令使用特殊的ModR/M编码)
          if (instruction.modrm.mod === 3 && instruction.modrm.rm === 0) {
            switch (instruction.modrm.reg) {
              case 0: // XGETBV - 读取扩展控制寄存器
                this.executeXgetbv();
                break;
              case 1: // XSETBV - 设置扩展控制寄存器
                this.executeXsetbv();
                break;
              default:
                break;
            }
          }
          // 检查是否是MWAIT指令
          else if (instruction.modrm.mod === 3 && instruction.modrm.rm === 1 && instruction.modrm.reg === 1) {
            this.executeMwait(instruction);
          }
          // 检查是否是INVLPG指令
          else if (instruction.modrm && instruction.modrm.mod === 0 && instruction.modrm.rm === 7 && instruction.modrm.reg === 7) {
            this.executeInvlpg(instruction);
          }
        }
        break;
      case 0x0F08: // 由于0x0F08已被LSS指令使用，我们需要在其他位置处理INVD指令
        // 检查是否是INVD指令，通过ModR/M或其他特殊编码区分
        if (!instruction.modrm) {
          this.executeInvd();
          break;
        }
        // 否则继续执行LSS指令
        this.executeLss(instruction);
        break;
      case 0x0FA2: // CPUID - CPU特性识别指令
        this.executeCpuid();
        break;
      case 0x0FAF: // IMUL r64, r/m64 - 有符号乘法（双操作数形式）
        this.executeImulRRm(instruction);
        break;
      case 0x62: // BOUND r/m16, m16:16 或 BOUND r/m32, m32:32
        if (instruction.modrm) {
          // 获取目标寄存器值
          let targetValue: u64;
          if (instruction.modrm.mod === 3) {
            // 目标是寄存器
            targetValue = this.registerManager.getRegister(instruction.modrm.rm);
          } else {
            // 目标是内存
            const targetAddress = this.calculateEffectiveAddress(instruction);
            // BOUND指令在64位模式下默认操作32位值
            targetValue = this.readDwordFromMemory(targetAddress);
          }

          // 计算包含边界值的内存地址（需要先修改modrm以获取正确的边界地址）
          // 创建一个新的modrm对象，将mod设置为0（表示内存操作），rm保持不变
          const boundsModrm = { ...instruction.modrm, mod: 0 };
          const boundsInstruction = { ...instruction, modrm: boundsModrm };
          const boundsAddress = this.calculateEffectiveAddress(boundsInstruction);

          // 读取下界和上界（32位值）
          const lowerBoundBytes = this.memoryManager.read(boundsAddress, 4);
          const upperBoundBytes = this.memoryManager.read(boundsAddress + 4n, 4);

          // 将字节数组转换为大整数
          const lowerBound = BigInt(new DataView(lowerBoundBytes.buffer).getInt32(0, true));
          const upperBound = BigInt(new DataView(upperBoundBytes.buffer).getInt32(0, true));

          // 执行边界检查
          this.executeBound(targetValue, lowerBound, upperBound);
        }
        break;
      case 0x6B: // IMUL r64, r/m64, imm8 - 有符号乘法（三操作数形式，立即数8位）
        this.executeImulRRImm(instruction);
        break;
      case 0x69: // IMUL r64, r/m64, imm32 - 有符号乘法（三操作数形式，立即数32位）
        this.executeImulRRImm(instruction);
        break;
      case 0x84: // TEST r/m64, r64
        this.executeTestRmR(instruction);
        break;
      case 0x0FB6: // MOVZX r64, r/m8 - 零扩展字节到四字
        this.executeMovzx(instruction);
        break;
      case 0x0FB7: // MOVZX r64, r/m16 - 零扩展字到四字
        this.executeMovzx(instruction);
        break;
      case 0x0FBE: // MOVSX r64, r/m8 - 符号扩展字节到四字
        this.executeMovsx(instruction);
        break;
      case 0x0FBF: // MOVSX r64, r/m16 - 符号扩展字到四字
        this.executeMovsx(instruction);
        break;
      case 0x480FBE: // MOVSXD r64, r/m32 - 符号扩展双字到四字
        this.executeMovsxd(instruction);
        break;
      case 0x480FB8: // BSWAP r64 - 字节交换（64位寄存器）
        this.executeBswap(instruction);
        break;
      case 0x0FC7: // CMPXCHG8B m64 - 比较并交换8字节
        this.executeCmpXchg8b(instruction);
        break;
      case 0x480FC7: // CMPXCHG16B m128 - 比较并交换16字节（64位模式）
        this.executeCmpXchg16b(instruction);
        break;
      case 0x85: // TEST r64, r/m64
        this.executeTestRRm(instruction);
        break;
      case 0x8D: // LEA r64, m
        this.executeLea(instruction);
        break;
      case 0x0FBD: // LZCNT r64, r/m64 - 前导零计数
        this.executeLzcnt(instruction);
        break;
      case 0xF6: // TEST r/m64, imm8
        if (instruction.modrm) {
          if (instruction.modrm.reg === 0) {
            this.executeTestImm(instruction);
          }
        }
        break;
      case 0xFF: // 多用途指令 (需要检查ModR/M的reg字段)
        if (instruction.modrm) {
          switch (instruction.modrm.reg) {
            case 0: // DEC r/m64
              this.executeDecRm(instruction);
              break;
            case 4:
              if (instruction.modrm.mod === 3 && instruction.modrm.rm === 4) {
                // JMP reg/mem64
                this.executeJmpRM(instruction);
              } else {
                // INC r/m64
                this.executeIncRm(instruction);
              }
              break;
          }
        }
        break;
      case 0x1A4: // SHLD r/m64, r/m64, imm8 - 双精度左移指令（立即数）
        this.executeShldImm(instruction);
        break;
      case 0x1A5: // SHLD r/m64, r/m64, CL - 双精度左移指令（CL寄存器）
        this.executeShldCl(instruction);
        break;
      case 0x1AC: // SHRD r/m64, r/m64, imm8 - 双精度右移指令（立即数）
        this.executeShrdImm(instruction);
        break;
      case 0x1AD: // SHRD r/m64, r/m64, CL - 双精度右移指令（CL寄存器）
        this.executeShrdCl(instruction);
        break;
      default:
        console.warn(`Unhandled opcode: 0x${opcode.toString(16)}`);
        // 对于未处理的指令，简单地前进到下一条指令
        break;
    }
  }

  // NOP指令
  private executeNop(): void {
    // NOP指令不执行任何操作，只是占用一个字节
  }

  // PUSH寄存器指令
  private executePushRegister(regIndex: number): void {
    const value = this.registerManager.getRegister(regIndex);
    this.push(value);
  }

  // POP寄存器指令
  private executePopRegister(regIndex: number): void {
    const value = this.pop();
    this.registerManager.setRegister(regIndex, value);
  }

  // 系统调用和时间戳计数器指令已在文件后面实现

  // AL寄存器与立即数的减法操作 (OpAluAlIbSub)的实现
  private executeAluAlIbSub(instruction: DecodedInstruction): void {
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const immValue = BigInt(instruction.immediate || 0) & 0xFFn;
    const result = alValue - immValue;

    // 只更新RAX的低8位
    const raxValue = this.registerManager.getRegister(Registers.RAX);
    this.registerManager.setRegister(Registers.RAX, (raxValue & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
    this.updateFlagsForSub(alValue, immValue, result & 0xFFn);
  }

  // AL寄存器与立即数的比较和测试操作 (OpCmpAlIb, OpTestAlIb)的实现
  private executeAluAlIbCompareTest(instruction: DecodedInstruction, opcode: number): void {
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const immValue = BigInt(instruction.immediate || 0) & 0xFFn;
    let result = 0n;

    switch (opcode) {
      case 0x3C: // CMP AL, imm8 (OpCmpAlIb)
        result = alValue - immValue;
        this.updateFlagsForSub(alValue, immValue, result & 0xFFn);
        break;
      case 0xA8: // TEST AL, imm8 (OpTestAlIb)
        result = alValue & immValue;
        this.updateFlagsForLogic(alValue, immValue, result & 0xFFn);
        break;
    }
  }

  // AL寄存器与立即数的AND和XOR操作 (OpAluAlIbAnd, OpAluAlIbXor)的实现
  private executeAluAlIbAndXor(instruction: DecodedInstruction, opcode: number): void {
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const immValue = BigInt(instruction.immediate || 0) & 0xFFn;
    let result = 0n;

    switch (opcode) {
      case 0x24: // AND AL, imm8 (OpAluAlIbAnd)
        result = alValue & immValue;
        // 只更新RAX的低8位
        const raxValue1 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue1 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForLogic(alValue, immValue, result & 0xFFn);
        break;
      case 0x34: // XOR AL, imm8 (OpAluAlIbXor)
        result = alValue ^ immValue;
        // 只更新RAX的低8位
        const raxValue2 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue2 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForLogic(alValue, immValue, result & 0xFFn);
        break;
    }
  }

  // AL寄存器与立即数的ALU操作 (OpAluAlIb系列)的实现
  private executeAluAlIb(instruction: DecodedInstruction, aluOpIndex: number): void {
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const immValue = BigInt(instruction.immediate || 0) & 0xFFn;
    let result = 0n;

    switch (aluOpIndex) {
      case 0: // ADD AL, imm8 (0x04)
        result = alValue + immValue;
        // 只更新RAX的低8位
        const raxValue3 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue3 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForAdd(alValue, immValue, result & 0xFFn);
        break;
      case 1: // OR AL, imm8 (0x05)
        result = alValue | immValue;
        // 只更新RAX的低8位
        const raxValue4 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue4 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForLogic(alValue, immValue, result & 0xFFn);
        break;
      case 2: // ADC AL, imm8 (0x06)
        result = alValue + immValue + (this.registerManager.getFlag('CF') ? 1n : 0n);
        // 只更新RAX的低8位
        const raxValue5 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue5 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForAddWithCarry(alValue, immValue, result & 0xFFn);
        break;
      case 3: // SBB AL, imm8 (0x07)
        result = alValue - immValue - (this.registerManager.getFlag('CF') ? 1n : 0n);
        // 只更新RAX的低8位
        const raxValue6 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue6 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForSubWithBorrow(alValue, immValue, result & 0xFFn);
        break;
    }
  }

  // 扩展的AL寄存器与立即数的ALU操作 (OpAluAlIb系列)的实现
  private executeAluAlIbExtended(instruction: DecodedInstruction, aluOpIndex: number): void {
    if (!instruction.modrm) return;

    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const immValue = BigInt(instruction.immediate || 0) & 0xFFn;
    let result = 0n;

    switch (aluOpIndex) {
      case 0: // ADD AL, imm8 (0x80 /r)
        result = alValue + immValue;
        // 只更新RAX的低8位
        const raxValue7 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue7 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForAdd(alValue, immValue, result & 0xFFn);
        break;
      case 1: // OR AL, imm8 (0x81 /r)
        result = alValue | immValue;
        // 只更新RAX的低8位
        const raxValue8 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue8 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForLogic(alValue, immValue, result & 0xFFn);
        break;
      case 2: // ADC AL, imm8 (0x82 /r)
        result = alValue + immValue + (this.registerManager.getFlag('CF') ? 1n : 0n);
        // 只更新RAX的低8位
        const raxValue9 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue9 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForAddWithCarry(alValue, immValue, result & 0xFFn);
        break;
      case 3: // SBB AL, imm8 (0x83 /r)
        result = alValue - immValue - (this.registerManager.getFlag('CF') ? 1n : 0n);
        // 只更新RAX的低8位
        const raxValue10 = this.registerManager.getRegister(Registers.RAX);
        this.registerManager.setRegister(Registers.RAX, (raxValue10 & 0xFFFFFFFFFFFFFF00n) | (result & 0xFFn));
        this.updateFlagsForSubWithBorrow(alValue, immValue, result & 0xFFn);
        break;
    }
  }

  // 32位ALU翻转操作指令 (OpAludFlip)的实现
  private executeAludFlip(instruction: DecodedInstruction, aluOpIndex: number): void {
    if (!instruction.modrm) return;

    // 翻转操作数顺序：源操作数变为目标，目标操作数变为源
    const srcAddr = this.calculateEffectiveAddress(instruction);
    const srcValue = this.readDwordFromMemory(srcAddr) & 0xFFFFFFFFn;
    const dstReg = instruction.modrm.reg;
    const dstValue = this.registerManager.getRegister(dstReg) & 0xFFFFFFFFn;

    let result = 0n;

    // 根据ALU操作索引执行相应的32位运算
    switch (aluOpIndex) {
      case 0: // ADD翻转 (0x10, 32位)
        result = srcValue + dstValue;
        this.writeDwordToMemory(srcAddr, result & 0xFFFFFFFFn);
        this.updateFlagsForAdd(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
      case 1: // OR翻转 (0x11, 32位)
        result = srcValue | dstValue;
        this.writeDwordToMemory(srcAddr, result & 0xFFFFFFFFn);
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
      case 2: // ADC翻转 (0x12, 32位)
        result = srcValue + dstValue + (this.registerManager.getFlag('CF') ? 1n : 0n);
        this.writeDwordToMemory(srcAddr, result & 0xFFFFFFFFn);
        this.updateFlagsForAddWithCarry(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
      case 3: // SBB翻转 (0x13, 32位)
        result = srcValue - dstValue - (this.registerManager.getFlag('CF') ? 1n : 0n);
        this.writeDwordToMemory(srcAddr, result & 0xFFFFFFFFn);
        this.updateFlagsForSubWithBorrow(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
      case 4: // AND翻转 (0x14, 32位)
        result = srcValue & dstValue;
        this.writeDwordToMemory(srcAddr, result & 0xFFFFFFFFn);
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
      case 5: // SUB翻转 (0x15, 32位)
        result = srcValue - dstValue;
        this.writeDwordToMemory(srcAddr, result & 0xFFFFFFFFn);
        this.updateFlagsForSub(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
      case 6: // XOR翻转 (0x16, 32位)
        result = srcValue ^ dstValue;
        this.writeDwordToMemory(srcAddr, result & 0xFFFFFFFFn);
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
      case 7: // CMP翻转 (0x17, 32位) - 注意CMP不修改操作数
        result = srcValue - dstValue;
        this.updateFlagsForSub(srcValue, dstValue, result & 0xFFFFFFFFn);
        break;
    }
  }

  // 16位ALU翻转操作指令 (OpAluwFlip)的实现
  private executeAluwFlip(instruction: DecodedInstruction, aluOpIndex: number): void {
    if (!instruction.modrm) return;

    // 翻转操作数顺序：源操作数变为目标，目标操作数变为源
    const srcAddr = this.calculateEffectiveAddress(instruction);
    const srcValue = this.readWordFromMemory(srcAddr) & 0xFFFFn;
    const dstReg = instruction.modrm.reg;
    const dstValue = this.registerManager.getRegister(dstReg) & 0xFFFFn;

    let result = 0n;

    // 根据ALU操作索引执行相应的16位运算
    switch (aluOpIndex) {
      case 0: // ADD翻转 (0x10 + 操作数大小前缀)
        result = srcValue + dstValue;
        this.writeWordToMemory(srcAddr, Number(result & 0xFFFFn));
        this.updateFlagsForAdd(srcValue, dstValue, result & 0xFFFFn);
        break;
      case 1: // OR翻转 (0x11 + 操作数大小前缀)
        result = srcValue | dstValue;
        this.writeWordToMemory(srcAddr, Number(result & 0xFFFFn));
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFFFn);
        break;
      case 2: // ADC翻转 (0x12 + 操作数大小前缀)
        result = srcValue + dstValue + (this.registerManager.getFlag('CF') ? 1n : 0n);
        this.writeWordToMemory(srcAddr, Number(result & 0xFFFFn));
        this.updateFlagsForAddWithCarry(srcValue, dstValue, result & 0xFFFFn);
        break;
      case 3: // SBB翻转 (0x13 + 操作数大小前缀)
        result = srcValue - dstValue - (this.registerManager.getFlag('CF') ? 1n : 0n);
        this.writeWordToMemory(srcAddr, Number(result & 0xFFFFn));
        this.updateFlagsForSubWithBorrow(srcValue, dstValue, result & 0xFFFFn);
        break;
      case 4: // AND翻转 (0x14 + 操作数大小前缀)
        result = srcValue & dstValue;
        this.writeWordToMemory(srcAddr, Number(result & 0xFFFFn));
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFFFn);
        break;
      case 5: // SUB翻转 (0x15 + 操作数大小前缀)
        result = srcValue - dstValue;
        this.writeWordToMemory(srcAddr, Number(result & 0xFFFFn));
        this.updateFlagsForSub(srcValue, dstValue, result & 0xFFFFn);
        break;
      case 6: // XOR翻转 (0x16 + 操作数大小前缀)
        result = srcValue ^ dstValue;
        this.writeWordToMemory(srcAddr, Number(result & 0xFFFFn));
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFFFn);
        break;
      case 7: // CMP翻转 (0x17 + 操作数大小前缀) - 注意CMP不修改操作数
        result = srcValue - dstValue;
        this.updateFlagsForSub(srcValue, dstValue, result & 0xFFFFn);
        break;
    }
  }

  // 8位ALU翻转操作指令 (OpAlubFlip)的实现
  private executeAlubFlip(instruction: DecodedInstruction, aluOpIndex: number): void {
    if (!instruction.modrm) return;

    // 翻转操作数顺序：源操作数变为目标，目标操作数变为源
    const srcAddr = this.calculateEffectiveAddress(instruction);
    const srcValue = BigInt(this.readByteFromMemory(srcAddr)) & 0xFFn;
    const dstReg = instruction.modrm.reg;
    const dstValue = this.registerManager.getRegister(dstReg) & 0xFFn;

    let result = 0n;

    // 根据ALU操作索引执行相应的8位运算
    switch (aluOpIndex) {
      case 0: // ADD翻转 (0x10)
        result = srcValue + dstValue;
        this.writeByteToMemory(srcAddr, Number(result & 0xFFn));
        this.updateFlagsForAdd(srcValue, dstValue, result & 0xFFn);
        break;
      case 1: // OR翻转 (0x11)
        result = srcValue | dstValue;
        this.writeByteToMemory(srcAddr, Number(result & 0xFFn));
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFn);
        break;
      case 2: // ADC翻转 (0x12)
        result = srcValue + dstValue + (this.registerManager.getFlag('CF') ? 1n : 0n);
        this.writeByteToMemory(srcAddr, Number(result & 0xFFn));
        this.updateFlagsForAddWithCarry(srcValue, dstValue, result & 0xFFn);
        break;
      case 3: // SBB翻转 (0x13)
        result = srcValue - dstValue - (this.registerManager.getFlag('CF') ? 1n : 0n);
        this.writeByteToMemory(srcAddr, Number(result & 0xFFn));
        this.updateFlagsForSubWithBorrow(srcValue, dstValue, result & 0xFFn);
        break;
      case 4: // AND翻转 (0x14)
        result = srcValue & dstValue;
        this.writeByteToMemory(srcAddr, Number(result & 0xFFn));
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFn);
        break;
      case 5: // SUB翻转 (0x15)
        result = srcValue - dstValue;
        this.writeByteToMemory(srcAddr, Number(result & 0xFFn));
        this.updateFlagsForSub(srcValue, dstValue, result & 0xFFn);
        break;
      case 6: // XOR翻转 (0x16)
        result = srcValue ^ dstValue;
        this.writeByteToMemory(srcAddr, Number(result & 0xFFn));
        this.updateFlagsForLogic(srcValue, dstValue, result & 0xFFn);
        break;
      case 7: // CMP翻转 (0x17) - 注意CMP不修改操作数
        result = srcValue - dstValue;
        this.updateFlagsForSub(srcValue, dstValue, result & 0xFFn);
        break;
    }
  }

  // LAHF - 将标志寄存器低字节加载到AH寄存器
  private executeLahf(instruction: DecodedInstruction): void {
    // 获取标志寄存器的低字节
    const flagsRegister = this.registerManager.getFlagsRegister();
    // 提取标志寄存器的低8位（LAHF只关心低8位中的部分标志位）
    // 注意：LAHF只提取CF, PF, AF, ZF, SF标志位并放入AH的对应位置
    // 具体来说：CF→AH.0, 1→AH.1, PF→AH.2, 0→AH.3, AF→AH.4, 0→AH.5, ZF→AH.6, SF→AH.7
    const ahValue = (
      ((this.registerManager.getFlag('CF') ? 1n : 0n) << 0n) |  // CF到位0
      (1n << 1n) |  // 位1恒为1
      ((this.registerManager.getFlag('PF') ? 1n : 0n) << 2n) |  // PF到位2
      (0n << 3n) |  // 位3恒为0
      ((this.registerManager.getFlag('AF') ? 1n : 0n) << 4n) |  // AF到位4
      (0n << 5n) |  // 位5恒为0
      ((this.registerManager.getFlag('ZF') ? 1n : 0n) << 6n) |  // ZF到位6
      ((this.registerManager.getFlag('SF') ? 1n : 0n) << 7n)    // SF到位7
    ) & 0xFFn;  // 确保是8位值

    // 获取当前RAX的值，并只更新AH部分
    const raxValue = this.registerManager.getRegister(0);
    const newValue = (raxValue & 0xFFFFFFFFFFFFFF00n) | ahValue;
    this.registerManager.setRegister(0, newValue);
  }

  // SAHF - 将AH寄存器存储到标志寄存器低字节
  private executeSahf(instruction: DecodedInstruction): void {
    // 获取RAX寄存器的AH部分（位8-15）
    const raxValue = this.registerManager.getRegister(0);
    const ahValue = (raxValue >> 8n) & 0xFFn;

    // 从AH寄存器中提取标志位并设置到标志寄存器
    // 注意：SAHF只设置CF, PF, AF, ZF, SF标志位
    this.registerManager.setFlag('CF', (ahValue & (1n << 0n)) !== 0n);  // 位0设置CF
    this.registerManager.setFlag('PF', (ahValue & (1n << 2n)) !== 0n);  // 位2设置PF
    this.registerManager.setFlag('AF', (ahValue & (1n << 4n)) !== 0n);  // 位4设置AF
    this.registerManager.setFlag('ZF', (ahValue & (1n << 6n)) !== 0n);  // 位6设置ZF
    this.registerManager.setFlag('SF', (ahValue & (1n << 7n)) !== 0n);  // 位7设置SF
  }

  // PUSHF/PUSHFD/PUSHFQ - 压入标志寄存器
  private executePushf(instruction: DecodedInstruction): void {
    // 在64位模式下，PUSHFQ将64位RFLAGS寄存器压入栈
    // 在32位模式下，PUSHFD将32位EFLAGS寄存器压入栈
    // 在16位模式下，PUSHF将16位FLAGS寄存器压入栈
    // 这里根据机器模式获取适当的标志寄存器值
    const flagsRegister = this.registerManager.getFlagsRegister();

    // 压入栈
    this.push(flagsRegister);
  }

  // POPF/POPFD/POPFQ - 弹出标志寄存器
  private executePopf(instruction: DecodedInstruction): void {
    // 从栈中弹出值
    const value = this.pop();

    // 根据机器模式设置适当的标志寄存器
    this.registerManager.setFlagsRegister(value);
  }

  // MOV寄存器，立即数指令
  private executeMovRegisterImmediate(regIndex: number, immediate: u64): void {
    this.registerManager.setRegister(regIndex, immediate);
  }

  // MOV r/m64, r64指令
  private executeMovRmToR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    const srcValue = this.registerManager.getRegister(srcReg);
    const dstAddr = this.calculateEffectiveAddress(instruction);

    this.writeQwordToMemory(dstAddr, srcValue);
  }

  // MOV r64, r/m64指令
  private executeMovRToRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    const srcAddr = this.calculateEffectiveAddress(instruction);
    const srcValue = this.readQwordFromMemory(srcAddr);

    this.registerManager.setRegister(dstReg, srcValue);
  }

  // ADD r/m64, r64指令 - 64位变体
  private executeAddRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    const srcValue = this.registerManager.getRegister(srcReg);
    const dstAddr = this.calculateEffectiveAddress(instruction);
    const dstValue = this.readQwordFromMemory(dstAddr);

    const result = dstValue + srcValue;
    this.writeQwordToMemory(dstAddr, result);

    // 更新标志位
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADD r/m8, r8指令 - 8位变体
  private executeAddRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源寄存器的低8位
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 读取8位目标值
    const dstValue = BigInt(this.readByteFromMemory(dstAddr));

    // 执行8位加法，并保持在8位范围内
    const result = (dstValue + srcValue) & 0xFFn;
    // 写回8位结果
    this.writeByteToMemory(dstAddr, Number(result));

    // 更新标志位，使用8位无符号值进行计算
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADD r/m16, r16指令 - 16位变体
  private executeAddRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源寄存器的低16位
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 读取16位目标值
    const dstValue = this.readWordFromMemory(dstAddr);

    // 执行16位加法，并保持在16位范围内
    const result = (dstValue + srcValue) & 0xFFFFn;
    // 写回16位结果
    this.writeWordToMemory(dstAddr, Number(result));

    // 更新标志位，使用16位无符号值进行计算
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADD r/m32, r32指令 - 32位变体
  private executeAddRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源寄存器的低32位
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFFFFFFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 读取32位目标值
    const dstValue = this.readDwordFromMemory(dstAddr);

    // 执行32位加法，并保持在32位范围内
    const result = (dstValue + srcValue) & 0xFFFFFFFFn;
    // 写回32位结果
    this.writeDwordToMemory(dstAddr, result);

    // 更新标志位，使用32位无符号值进行计算
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADD r64, r/m64指令 - 64位变体
  private executeAddRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    const dstValue = this.registerManager.getRegister(dstReg);
    const srcAddr = this.calculateEffectiveAddress(instruction);
    const srcValue = this.readQwordFromMemory(srcAddr);

    const result = dstValue + srcValue;
    this.registerManager.setRegister(dstReg, result);

    // 更新标志位
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADD r8, r/m8指令 - 8位变体
  private executeAddRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(dstReg);
    // 获取目标寄存器的低8位
    const dstValue = currentValue & 0xFFn;
    const srcAddr = this.calculateEffectiveAddress(instruction);
    // 读取8位源值
    const srcValue = BigInt(this.readByteFromMemory(srcAddr));

    // 执行8位加法，并保持在8位范围内
    const result = (dstValue + srcValue) & 0xFFn;
    // 只更新寄存器的低8位
    const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
    this.registerManager.setRegister(dstReg, newValue);

    // 更新标志位，使用8位无符号值进行计算
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADD r16, r/m16指令 - 16位变体
  private executeAddRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(dstReg);
    // 获取目标寄存器的低16位
    const dstValue = currentValue & 0xFFFFn;
    const srcAddr = this.calculateEffectiveAddress(instruction);
    // 读取16位源值
    const srcValue = this.readWordFromMemory(srcAddr);

    // 执行16位加法，并保持在16位范围内
    const result = (dstValue + srcValue) & 0xFFFFn;
    // 只更新寄存器的低16位
    const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
    this.registerManager.setRegister(dstReg, newValue);

    // 更新标志位，使用16位无符号值进行计算
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADD r32, r/m32指令 - 32位变体
  private executeAddRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(dstReg);
    // 获取目标寄存器的低32位
    const dstValue = currentValue & 0xFFFFFFFFn;
    const srcAddr = this.calculateEffectiveAddress(instruction);
    // 读取32位源值
    const srcValue = this.readDwordFromMemory(srcAddr);

    // 执行32位加法，并保持在32位范围内
    const result = (dstValue + srcValue) & 0xFFFFFFFFn;
    // 只更新寄存器的低32位
    const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
    this.registerManager.setRegister(dstReg, newValue);

    // 更新标志位，使用32位无符号值进行计算
    this.updateFlagsForAdd(dstValue, srcValue, result);
  }

  // ADC r/m64, r64 - 带进位加法指令 - 64位变体
  private executeAdcRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法
    const result = destValue + srcValue + carryValue;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    // 注意：ADC的标志位更新与ADD类似，但要考虑进位输入
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // ADC r/m8, r8 - 带进位加法指令 - 8位变体
  private executeAdcRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器的低8位
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法，并保持在8位范围内
    const result = (destValue + srcValue + carryValue) & 0xFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低8位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeByteToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // ADC r/m16, r16 - 带进位加法指令 - 16位变体
  private executeAdcRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器的低16位
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readWordFromMemory(memoryAddress);
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法，并保持在16位范围内
    const result = (destValue + srcValue + carryValue) & 0xFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低16位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeWordToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // ADC r/m32, r32 - 带进位加法指令 - 32位变体
  private executeAdcRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器的低32位
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readDwordFromMemory(memoryAddress);
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法，并保持在32位范围内
    const result = (destValue + srcValue + carryValue) & 0xFFFFFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低32位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeDwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // ADC r64, r/m64 - 带进位加法指令 - 64位变体
  private executeAdcRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值
    const destValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法
    const result = destValue + srcValue + carryValue;

    // 写回结果到目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, result);

    // 更新标志位
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // ADC r8, r/m8 - 带进位加法指令 - 8位变体
  private executeAdcRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标寄存器的低8位
    const destValue = currentValue & 0xFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法，并保持在8位范围内
    const result = (destValue + srcValue + carryValue) & 0xFFn;

    // 只更新寄存器的低8位
    const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // ADC r16, r/m16 - 带进位加法指令 - 16位变体
  private executeAdcRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标寄存器的低16位
    const destValue = currentValue & 0xFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readWordFromMemory(memoryAddress);
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法，并保持在16位范围内
    const result = (destValue + srcValue + carryValue) & 0xFFFFn;

    // 只更新寄存器的低16位
    const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // ADC r32, r/m32 - 带进位加法指令 - 32位变体
  private executeAdcRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标寄存器的低32位
    const destValue = currentValue & 0xFFFFFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readDwordFromMemory(memoryAddress);
    }

    // 获取进位标志
    const carryValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带进位加法，并保持在32位范围内
    const result = (destValue + srcValue + carryValue) & 0xFFFFFFFFn;

    // 只更新寄存器的低32位
    const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForAdd(destValue + carryValue, srcValue, result);
  }

  // SUB r/m64, r64指令 - 64位变体
  private executeSubRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    const srcValue = this.registerManager.getRegister(srcReg);
    const dstAddr = this.calculateEffectiveAddress(instruction);
    const dstValue = this.readQwordFromMemory(dstAddr);

    const result = dstValue - srcValue;
    this.writeQwordToMemory(dstAddr, result);

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // SUB r/m8, r8指令 - 8位变体
  private executeSubRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源值（只取低8位）
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 获取目标值
    let dstValue: u64;
    let result: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取8位值
      dstValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
      // 执行减法并保持在8位范围内
      result = (dstValue - srcValue) & 0xFFn;
      // 只更新寄存器的低8位
      const fullValue = (this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(instruction.modrm.rm, fullValue);
    } else {
      // 从内存读取8位值
      dstValue = BigInt(this.readByteFromMemory(dstAddr));
      // 执行减法并保持在8位范围内
      result = (dstValue - srcValue) & 0xFFn;
      // 写入8位结果到内存
      this.writeByteToMemory(dstAddr, Number(result));
    }

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // SUB r/m16, r16指令 - 16位变体
  private executeSubRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源值（只取低16位）
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 获取目标值
    let dstValue: u64;
    let result: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取16位值
      dstValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
      // 执行减法并保持在16位范围内
      result = (dstValue - srcValue) & 0xFFFFn;
      // 只更新寄存器的低16位
      const fullValue = (this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(instruction.modrm.rm, fullValue);
    } else {
      // 从内存读取16位值
      dstValue = this.readWordFromMemory(dstAddr);
      // 执行减法并保持在16位范围内
      result = (dstValue - srcValue) & 0xFFFFn;
      // 写入16位结果到内存
      this.writeWordToMemory(dstAddr, Number(result));
    }

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // SUB r/m32, r32指令 - 32位变体
  private executeSubRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源值（只取低32位）
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFFFFFFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 获取目标值
    let dstValue: u64;
    let result: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取32位值
      dstValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
      // 执行减法并保持在32位范围内
      result = (dstValue - srcValue) & 0xFFFFFFFFn;
      // 只更新寄存器的低32位
      const fullValue = (this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(instruction.modrm.rm, fullValue);
    } else {
      // 从内存读取32位值
      dstValue = this.readDwordFromMemory(dstAddr);
      // 执行减法并保持在32位范围内
      result = (dstValue - srcValue) & 0xFFFFFFFFn;
      // 写入32位结果到内存
      this.writeDwordToMemory(dstAddr, result);
    }

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // 更新逻辑指令的标志位
  private updateFlagsForLogic(src: u64, dst: u64, result: u64): void {
    // CF (Carry Flag) - 逻辑运算总是清除CF
    this.registerManager.setFlag('CF', false);

    // OF (Overflow Flag) - 逻辑运算总是清除OF
    this.registerManager.setFlag('OF', false);

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', Number(result) < 0);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(result);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);
  }

  // 计算64位整数中设置的位的个数
  private countSetBits(value: u64): number {
    let count = 0;
    let num = value;
    while (num > 0n) {
      count++;
      num &= num - 1n;
    }
    return count;
  }


  // SUB r64, r/m64指令 - 64位变体
  private executeSubRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    const dstValue = this.registerManager.getRegister(dstReg);
    const srcAddr = this.calculateEffectiveAddress(instruction);
    const srcValue = this.readQwordFromMemory(srcAddr);

    const result = dstValue - srcValue;
    this.registerManager.setRegister(dstReg, result);

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // SUB r8, r/m8指令 - 8位变体
  private executeSubRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(dstReg);
    // 获取目标值（只取低8位）
    const dstValue = currentValue & 0xFFn;
    const srcAddr = this.calculateEffectiveAddress(instruction);
    // 获取源值
    let srcValue: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取8位值
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
    } else {
      // 从内存读取8位值
      srcValue = BigInt(this.readByteFromMemory(srcAddr));
    }

    // 执行减法并保持在8位范围内
    const result = (dstValue - srcValue) & 0xFFn;

    // 只更新寄存器的低8位
    const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
    this.registerManager.setRegister(dstReg, newValue);

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // SUB r16, r/m16指令 - 16位变体
  private executeSubRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(dstReg);
    // 获取目标值（只取低16位）
    const dstValue = currentValue & 0xFFFFn;
    const srcAddr = this.calculateEffectiveAddress(instruction);
    // 获取源值
    let srcValue: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取16位值
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
    } else {
      // 从内存读取16位值
      srcValue = this.readWordFromMemory(srcAddr);
    }

    // 执行减法并保持在16位范围内
    const result = (dstValue - srcValue) & 0xFFFFn;

    // 只更新寄存器的低16位
    const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
    this.registerManager.setRegister(dstReg, newValue);

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // SUB r32, r/m32指令 - 32位变体
  private executeSubRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(dstReg);
    // 获取目标值（只取低32位）
    const dstValue = currentValue & 0xFFFFFFFFn;
    const srcAddr = this.calculateEffectiveAddress(instruction);
    // 获取源值
    let srcValue: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取32位值
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
    } else {
      // 从内存读取32位值
      srcValue = this.readDwordFromMemory(srcAddr);
    }

    // 执行减法并保持在32位范围内
    const result = (dstValue - srcValue) & 0xFFFFFFFFn;

    // 只更新寄存器的低32位
    const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
    this.registerManager.setRegister(dstReg, newValue);

    // 更新标志位
    this.updateFlagsForSub(dstValue, srcValue, result);
  }

  // SBB r/m64, r64 - 带借位减法指令 - 64位变体
  private executeSbbRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 获取借位标志（CF表示减法中的借位）
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法
    const result = destValue - srcValue - borrowValue;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    // 注意：SBB的标志位更新与SUB类似，但要考虑借位输入
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // SBB r/m8, r8 - 带借位减法指令 - 8位变体
  private executeSbbRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低8位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 获取借位标志（CF表示减法中的借位）
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法，并保持在8位范围内
    const result = (destValue - srcValue - borrowValue) & 0xFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低8位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      // 写入8位结果到内存
      this.writeByteToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    // 注意：SBB的标志位更新与SUB类似，但要考虑借位输入
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // SBB r/m16, r16 - 带借位减法指令 - 16位变体
  private executeSbbRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低16位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readWordFromMemory(memoryAddress);
    }

    // 获取借位标志（CF表示减法中的借位）
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法，并保持在16位范围内
    const result = (destValue - srcValue - borrowValue) & 0xFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低16位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      // 写入16位结果到内存
      this.writeWordToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    // 注意：SBB的标志位更新与SUB类似，但要考虑借位输入
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // SBB r/m32, r32 - 带借位减法指令 - 32位变体
  private executeSbbRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低32位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readDwordFromMemory(memoryAddress);
    }

    // 获取借位标志（CF表示减法中的借位）
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法，并保持在32位范围内
    const result = (destValue - srcValue - borrowValue) & 0xFFFFFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低32位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      // 写入32位结果到内存
      this.writeDwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    // 注意：SBB的标志位更新与SUB类似，但要考虑借位输入
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // SBB r64, r/m64 - 带借位减法指令 - 64位变体
  private executeSbbRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值
    const destValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 获取借位标志
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法
    const result = destValue - srcValue - borrowValue;

    // 写回结果到目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, result);

    // 更新标志位
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // SBB r8, r/m8 - 带借位减法指令 - 8位变体
  private executeSbbRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低8位）
    const destValue = currentValue & 0xFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 获取借位标志
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法，并保持在8位范围内
    const result = (destValue - srcValue - borrowValue) & 0xFFn;

    // 只更新寄存器的低8位
    const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // SBB r16, r/m16 - 带借位减法指令 - 16位变体
  private executeSbbRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低16位）
    const destValue = currentValue & 0xFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readWordFromMemory(memoryAddress);
    }

    // 获取借位标志
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法，并保持在16位范围内
    const result = (destValue - srcValue - borrowValue) & 0xFFFFn;

    // 只更新寄存器的低16位
    const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // SBB r32, r/m32 - 带借位减法指令 - 32位变体
  private executeSbbRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低32位）
    const destValue = currentValue & 0xFFFFFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readDwordFromMemory(memoryAddress);
    }

    // 获取借位标志
    const borrowValue = this.registerManager.getFlag('CF') ? 1n : 0n;

    // 执行带借位减法，并保持在32位范围内
    const result = (destValue - srcValue - borrowValue) & 0xFFFFFFFFn;

    // 只更新寄存器的低32位
    const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForSub(destValue - borrowValue, srcValue, result);
  }

  // AND r/m64, r64指令 - 64位变体
  private executeAndRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    const srcValue = this.registerManager.getRegister(srcReg);
    const dstAddr = this.calculateEffectiveAddress(instruction);
    const dstValue = this.readQwordFromMemory(dstAddr);

    const result = dstValue & srcValue;
    this.writeQwordToMemory(dstAddr, result);

    // 更新标志位
    this.updateFlagsForLogic(dstValue, srcValue, result);
  }

  // AND r/m8, r8指令 - 8位变体
  private executeAndRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源值（只取低8位）
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 获取目标值
    let dstValue: u64;
    let result: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取8位值
      dstValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
      // 执行AND操作并保持在8位范围内
      result = dstValue & srcValue;
      // 只更新寄存器的低8位
      const fullValue = (this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(instruction.modrm.rm, fullValue);
    } else {
      // 从内存读取8位值
      dstValue = BigInt(this.readByteFromMemory(dstAddr));
      // 执行AND操作并保持在8位范围内
      result = (dstValue & srcValue) & 0xFFn;
      // 写入8位结果到内存
      this.writeByteToMemory(dstAddr, Number(result));
    }

    // 更新标志位
    this.updateFlagsForLogic(dstValue, srcValue, result);
  }

  // AND r/m16, r16指令 - 16位变体
  private executeAndRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源值（只取低16位）
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 获取目标值
    let dstValue: u64;
    let result: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取16位值
      dstValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
      // 执行AND操作并保持在16位范围内
      result = dstValue & srcValue;
      // 只更新寄存器的低16位
      const fullValue = (this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(instruction.modrm.rm, fullValue);
    } else {
      // 从内存读取16位值
      dstValue = this.readWordFromMemory(dstAddr);
      // 执行AND操作并保持在16位范围内
      result = (dstValue & srcValue) & 0xFFFFn;
      // 写入16位结果到内存
      this.writeWordToMemory(dstAddr, Number(result));
    }

    // 更新标志位
    this.updateFlagsForLogic(dstValue, srcValue, result);
  }

  // AND r/m32, r32指令 - 32位变体
  private executeAndRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    // 获取源值（只取低32位）
    const srcValue = this.registerManager.getRegister(srcReg) & 0xFFFFFFFFn;
    const dstAddr = this.calculateEffectiveAddress(instruction);
    // 获取目标值
    let dstValue: u64;
    let result: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      // 从寄存器读取32位值
      dstValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
      // 执行AND操作并保持在32位范围内
      result = dstValue & srcValue;
      // 只更新寄存器的低32位
      const fullValue = (this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(instruction.modrm.rm, fullValue);
    } else {
      // 从内存读取32位值
      dstValue = this.readDwordFromMemory(dstAddr);
      // 执行AND操作并保持在32位范围内
      result = (dstValue & srcValue) & 0xFFFFFFFFn;
      // 写入32位结果到内存
      this.writeDwordToMemory(dstAddr, result);
    }

    // 更新标志位
    this.updateFlagsForLogic(dstValue, srcValue, result);
  }

  // AND r64, r/m64 指令实现 - 64位变体
  private executeAndRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值
    const destValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行AND操作
    const result = destValue & srcValue;

    // 写回结果到目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, result);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // AND r8, r/m8 指令实现 - 8位变体
  private executeAndRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低8位）
    const destValue = currentValue & 0xFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 执行AND操作并保持在8位范围内
    const result = (destValue & srcValue) & 0xFFn;

    // 只更新寄存器的低8位
    const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // AND r16, r/m16 指令实现 - 16位变体
  private executeAndRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低16位）
    const destValue = currentValue & 0xFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readWordFromMemory(memoryAddress);
    }

    // 执行AND操作并保持在16位范围内
    const result = (destValue & srcValue) & 0xFFFFn;

    // 只更新寄存器的低16位
    const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // AND r32, r/m32 指令实现 - 32位变体
  private executeAndRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低32位）
    const destValue = currentValue & 0xFFFFFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readDwordFromMemory(memoryAddress);
    }

    // 执行AND操作并保持在32位范围内
    const result = (destValue & srcValue) & 0xFFFFFFFFn;

    // 只更新寄存器的低32位
    const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // CMP r/m64, r64 指令实现（64位变体）
  private executeCmpRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取目标值
    let destValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // CMP r/m8, r8 指令实现（8位变体）
  private executeCmpRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低8位）
    const srcRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const srcValue = srcRegisterValue & 0xFFn;

    // 获取目标值
    let destValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      const destRegisterValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = destRegisterValue & 0xFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位（保持8位操作的语义）
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // CMP r/m16, r16 指令实现（16位变体）
  private executeCmpRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低16位）
    const srcRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const srcValue = srcRegisterValue & 0xFFFFn;

    // 获取目标值
    let destValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      const destRegisterValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = destRegisterValue & 0xFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readWordFromMemory(memoryAddress);
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位（保持16位操作的语义）
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // CMP r/m32, r32 指令实现（32位变体）
  private executeCmpRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低32位）
    const srcRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const srcValue = srcRegisterValue & 0xFFFFFFFFn;

    // 获取目标值
    let destValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      const destRegisterValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = destRegisterValue & 0xFFFFFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readDwordFromMemory(memoryAddress);
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位（保持32位操作的语义）
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // MUL r/m64 - 无符号乘法指令（64位变体）
  private executeMulRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m64）
    const srcValue = this.readQwordFromMemory(this.calculateEffectiveAddress(instruction));
    // 获取AX寄存器值（RAX）
    const axValue = this.registerManager.getRegister(Registers.RAX);

    // 执行64位无符号乘法，结果存储在RAX:RDX中
    // 注意：在JavaScript中，我们需要处理可能的溢出
    const result = BigInt.asUintN(128, axValue * srcValue);
    const raxResult = BigInt.asUintN(64, result);
    const rdxResult = BigInt.asUintN(64, result >> 64n);

    // 写回结果到RAX和RDX寄存器
    this.registerManager.setRegister(Registers.RAX, raxResult);
    this.registerManager.setRegister(Registers.RDX, rdxResult);

    // 更新标志位
    // CF和OF在MUL指令中设置为1如果结果的高位部分（RDX）不为零，否则设置为0
    // ZF、SF、AF和PF未定义，保持不变
    this.registerManager.setFlag('CF', rdxResult !== 0n);
    this.registerManager.setFlag('OF', rdxResult !== 0n);
  }

  // MUL r/m32 - 无符号乘法指令（32位变体）
  private executeMulRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m32）
    const srcValue = this.readDwordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFFFFFn;
    // 获取AX寄存器值（EAX）
    const axValue = this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn;

    // 执行32位无符号乘法，结果存储在EDX:EAX中
    const result = BigInt.asUintN(64, axValue * srcValue);
    const eaxResult = BigInt.asUintN(32, result);
    const edxResult = BigInt.asUintN(32, result >> 32n);

    // 写回结果到EAX和EDX寄存器（作为RAX和RDX的低32位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFF00000000n) | eaxResult);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFF00000000n) | edxResult);

    // 更新标志位
    // CF和OF在MUL指令中设置为1如果结果的高位部分（EDX）不为零，否则设置为0
    this.registerManager.setFlag('CF', edxResult !== 0n);
    this.registerManager.setFlag('OF', edxResult !== 0n);
  }

  // MUL r/m16 - 无符号乘法指令（16位变体）
  private executeMulRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m16）
    const srcValue = this.readWordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFn;
    // 获取AX寄存器值（AX）
    const axValue = this.registerManager.getRegister(Registers.RAX) & 0xFFFFn;

    // 执行16位无符号乘法，结果存储在DX:AX中
    const result = BigInt.asUintN(32, axValue * srcValue);
    const axResult = BigInt.asUintN(16, result);
    const dxResult = BigInt.asUintN(16, result >> 16n);

    // 写回结果到AX和DX寄存器（作为RAX和RDX的低16位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | axResult);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFFFFFF0000n) | dxResult);

    // 更新标志位
    // CF和OF在MUL指令中设置为1如果结果的高位部分（DX）不为零，否则设置为0
    this.registerManager.setFlag('CF', dxResult !== 0n);
    this.registerManager.setFlag('OF', dxResult !== 0n);
  }

  // MUL r/m8 - 无符号乘法指令（8位变体）
  private executeMulRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m8）
    const srcValue = BigInt(this.readByteFromMemory(this.calculateEffectiveAddress(instruction))) & 0xFFn;
    // 获取AL寄存器值（AL）
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;

    // 执行8位无符号乘法，结果存储在AX中
    const result = BigInt.asUintN(16, alValue * srcValue);
    const axResult = BigInt.asUintN(16, result);

    // 写回结果到AX寄存器（作为RAX的低16位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | axResult);

    // 更新标志位
    // CF和OF在MUL指令中设置为1如果结果的高位部分（AH）不为零，否则设置为0
    const ahResult = (axResult >> 8n) & 0xFFn;
    this.registerManager.setFlag('CF', ahResult !== 0n);
    this.registerManager.setFlag('OF', ahResult !== 0n);
  }

  // IMUL r/m64 - 有符号乘法指令（单操作数形式，64位变体）
  private executeImulRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m64）
    const srcValue = this.readQwordFromMemory(this.calculateEffectiveAddress(instruction));
    // 获取AX寄存器值（RAX）
    const axValue = this.registerManager.getRegister(Registers.RAX);

    // 执行64位有符号乘法，结果存储在RAX:RDX中
    // 将无符号bigint转换为有符号进行乘法操作
    const signedAx = BigInt.asIntN(64, axValue);
    const signedSrc = BigInt.asIntN(64, srcValue);
    const signedResult = signedAx * signedSrc;
    const result = BigInt.asUintN(128, signedResult);

    const raxResult = BigInt.asUintN(64, result);
    const rdxResult = BigInt.asUintN(64, result >> 64n);

    // 写回结果到RAX和RDX寄存器
    this.registerManager.setRegister(Registers.RAX, raxResult);
    this.registerManager.setRegister(Registers.RDX, rdxResult);

    // 更新标志位
    // CF和OF在IMUL指令中设置为1如果结果的高位部分（RDX）不是低位部分（RAX）的符号扩展，否则设置为0
    this.registerManager.setFlag('CF', rdxResult !== ((raxResult >> 63n) & 0xFFFFFFFFFFFFFFFFn));
    this.registerManager.setFlag('OF', rdxResult !== ((raxResult >> 63n) & 0xFFFFFFFFFFFFFFFFn));
  }

  // IMUL r/m32 - 有符号乘法指令（单操作数形式，32位变体）
  private executeImulRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m32）
    const srcValue = this.readDwordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFFFFFn;
    // 获取AX寄存器值（EAX）
    const axValue = this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn;

    // 执行32位有符号乘法，结果存储在EDX:EAX中
    const signedAx = BigInt.asIntN(32, axValue);
    const signedSrc = BigInt.asIntN(32, srcValue);
    const signedResult = signedAx * signedSrc;
    const result = BigInt.asUintN(64, signedResult);

    const eaxResult = BigInt.asUintN(32, result);
    const edxResult = BigInt.asUintN(32, result >> 32n);

    // 写回结果到EAX和EDX寄存器（作为RAX和RDX的低32位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFF00000000n) | eaxResult);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFF00000000n) | edxResult);

    // 更新标志位
    // CF和OF在IMUL指令中设置为1如果结果的高位部分（EDX）不是低位部分（EAX）的符号扩展，否则设置为0
    const signExtend = ((eaxResult >> 31n) & 0xFFFFFFFFn);
    this.registerManager.setFlag('CF', edxResult !== signExtend);
    this.registerManager.setFlag('OF', edxResult !== signExtend);
  }

  // IMUL r/m16 - 有符号乘法指令（单操作数形式，16位变体）
  private executeImulRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m16）
    const srcValue = this.readWordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFn;
    // 获取AX寄存器值（AX）
    const axValue = this.registerManager.getRegister(Registers.RAX) & 0xFFFFn;

    // 执行16位有符号乘法，结果存储在DX:AX中
    const signedAx = BigInt.asIntN(16, axValue);
    const signedSrc = BigInt.asIntN(16, srcValue);
    const signedResult = signedAx * signedSrc;
    const result = BigInt.asUintN(32, signedResult);

    const axResult = BigInt.asUintN(16, result);
    const dxResult = BigInt.asUintN(16, result >> 16n);

    // 写回结果到AX和DX寄存器（作为RAX和RDX的低16位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | axResult);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFFFFFF0000n) | dxResult);

    // 更新标志位
    // CF和OF在IMUL指令中设置为1如果结果的高位部分（DX）不是低位部分（AX）的符号扩展，否则设置为0
    const signExtend = ((axResult >> 15n) & 0xFFFFn);
    this.registerManager.setFlag('CF', dxResult !== signExtend);
    this.registerManager.setFlag('OF', dxResult !== signExtend);
  }

  // IMUL r/m8 - 有符号乘法指令（单操作数形式，8位变体）
  private executeImulRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r/m8）
    const srcValue = BigInt(this.readByteFromMemory(this.calculateEffectiveAddress(instruction))) & 0xFFn;
    // 获取AL寄存器值（AL）
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;

    // 执行8位有符号乘法，结果存储在AX中
    const signedAl = BigInt.asIntN(8, alValue);
    const signedSrc = BigInt.asIntN(8, srcValue);
    const signedResult = signedAl * signedSrc;
    const result = BigInt.asUintN(16, signedResult);
    const axResult = BigInt.asUintN(16, result);

    // 写回结果到AX寄存器（作为RAX的低16位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | axResult);

    // 更新标志位
    // CF和OF在IMUL指令中设置为1如果结果的高位部分（AH）不是低位部分（AL）的符号扩展，否则设置为0
    const alResult = axResult & 0xFFn;
    const ahResult = (axResult >> 8n) & 0xFFn;
    const signExtend = ((alResult >> 7n) & 0xFFn);
    this.registerManager.setFlag('CF', ahResult !== signExtend);
    this.registerManager.setFlag('OF', ahResult !== signExtend);
  }

  // IMUL r64, r/m64 - 有符号乘法指令（双操作数形式）
  private executeImulRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值
    const destValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行64位有符号乘法
    const signedDest = BigInt.asIntN(64, destValue);
    const signedSrc = BigInt.asIntN(64, srcValue);
    const signedResult = signedDest * signedSrc;
    const result = BigInt.asUintN(64, signedResult);

    // 写回结果到目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, result);

    // 更新标志位
    // CF和OF在IMUL指令中设置为1如果结果需要超过64位来表示，否则设置为0
    this.registerManager.setFlag('CF', signedResult !== BigInt.asIntN(64, signedResult));
    this.registerManager.setFlag('OF', signedResult !== BigInt.asIntN(64, signedResult));
    // ZF、SF、AF和PF未定义，保持不变
  }

  // DIV r/m64 - 无符号除法指令（64位变体）
  private executeDivRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m64）
    const divisor = this.readQwordFromMemory(this.calculateEffectiveAddress(instruction));
    // 获取被除数（RAX:RDX）
    const dividendLow = this.registerManager.getRegister(Registers.RAX);
    const dividendHigh = this.registerManager.getRegister(Registers.RDX);

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 构建128位无符号被除数
    const dividend = BigInt.asUintN(128, (dividendHigh << 64n) | dividendLow);

    // 执行无符号除法
    const quotient = BigInt.asUintN(64, dividend / divisor);
    const remainder = BigInt.asUintN(64, dividend % divisor);

    // 检查商是否溢出（对于64位除法，如果商超过64位则会溢出）
    // 在无符号除法中，如果dividendHigh >= divisor，则会发生溢出
    if (dividendHigh >= divisor) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到RAX（商）和RDX（余数）寄存器
    this.registerManager.setRegister(Registers.RAX, quotient);
    this.registerManager.setRegister(Registers.RDX, remainder);

    // 除法指令不更新任何标志位
  }

  // DIV r/m32 - 无符号除法指令（32位变体）
  private executeDivRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m32）
    const divisor = this.readDwordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFFFFFn;
    // 获取被除数（EAX:EDX）
    const dividendLow = this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn;
    const dividendHigh = this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFFn;

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 构建64位无符号被除数
    const dividend = BigInt.asUintN(64, (dividendHigh << 32n) | dividendLow);

    // 执行无符号除法
    const quotient = BigInt.asUintN(32, dividend / divisor);
    const remainder = BigInt.asUintN(32, dividend % divisor);

    // 检查商是否溢出（对于32位除法，如果商超过32位则会溢出）
    // 在无符号除法中，如果dividendHigh >= divisor，则会发生溢出
    if (dividendHigh >= divisor) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到EAX（商）和EDX（余数）寄存器（作为RAX和RDX的低32位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFF00000000n) | quotient);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFF00000000n) | remainder);

    // 除法指令不更新任何标志位
  }

  // DIV r/m16 - 无符号除法指令（16位变体）
  private executeDivRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m16）
    const divisor = this.readWordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFn;
    // 获取被除数（AX:DX）
    const dividendLow = this.registerManager.getRegister(Registers.RAX) & 0xFFFFn;
    const dividendHigh = this.registerManager.getRegister(Registers.RDX) & 0xFFFFn;

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 构建32位无符号被除数
    const dividend = BigInt.asUintN(32, (dividendHigh << 16n) | dividendLow);

    // 执行无符号除法
    const quotient = BigInt.asUintN(16, dividend / divisor);
    const remainder = BigInt.asUintN(16, dividend % divisor);

    // 检查商是否溢出（对于16位除法，如果商超过16位则会溢出）
    // 在无符号除法中，如果dividendHigh >= divisor，则会发生溢出
    if (dividendHigh >= divisor) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到AX（商）和DX（余数）寄存器（作为RAX和RDX的低16位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | quotient);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFFFFFF0000n) | remainder);

    // 除法指令不更新任何标志位
  }

  // DIV r/m8 - 无符号除法指令（8位变体）
  private executeDivRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m8）
    const divisor = BigInt(this.readByteFromMemory(this.calculateEffectiveAddress(instruction))) & 0xFFn;
    // 获取被除数（AL:AH）
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const ahValue = (this.registerManager.getRegister(Registers.RAX) >> 8n) & 0xFFn;

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 构建16位无符号被除数
    const dividend = BigInt.asUintN(16, (ahValue << 8n) | alValue);

    // 执行无符号除法
    const quotient = BigInt.asUintN(8, dividend / divisor);
    const remainder = BigInt.asUintN(8, dividend % divisor);

    // 检查商是否溢出（对于8位除法，如果商超过8位则会溢出）
    // 在无符号除法中，如果AH >= divisor，则会发生溢出
    if (ahValue >= divisor) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到AL（商）和AH（余数）寄存器（作为RAX的低16位）
    const axResult = (remainder << 8n) | quotient;
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | axResult);

    // 除法指令不更新任何标志位
  }

  // IDIV r/m64 - 有符号除法指令（64位变体）
  private executeIdivRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m64）
    const divisor = this.readQwordFromMemory(this.calculateEffectiveAddress(instruction));
    // 获取被除数（RAX:RDX）
    const dividendLow = this.registerManager.getRegister(Registers.RAX);
    const dividendHigh = this.registerManager.getRegister(Registers.RDX);

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 将无符号bigint转换为有符号进行除法操作
    // 对于有符号除法，RDX是RAX的符号扩展
    const signedDividendLow = BigInt.asIntN(64, dividendLow);
    const signedDivisor = BigInt.asIntN(64, divisor);

    // 构建128位有符号被除数（通过符号扩展）
    const signBit = signedDividendLow >> 63n;
    const signedDividendHigh = BigInt.asIntN(64, signBit);
    const signedDividend = BigInt.asIntN(128, (signedDividendHigh << 64n) | BigInt.asUintN(64, signedDividendLow));

    // 执行有符号除法
    const signedQuotient = signedDividend / signedDivisor;
    const signedRemainder = signedDividend % signedDivisor;

    // 转换回无符号bigint
    const quotient = BigInt.asUintN(64, signedQuotient);
    const remainder = BigInt.asUintN(64, signedRemainder);

    // 检查商是否溢出（如果商超出了64位有符号整数的范围）
    if (signedQuotient < BigInt.asIntN(64, -0x8000000000000000n) ||
      signedQuotient > BigInt.asIntN(64, 0x7FFFFFFFFFFFFFFFn)) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到RAX（商）和RDX（余数）寄存器
    this.registerManager.setRegister(Registers.RAX, quotient);
    this.registerManager.setRegister(Registers.RDX, remainder);

    // 除法指令不更新任何标志位
  }

  // IDIV r/m32 - 有符号除法指令（32位变体）
  private executeIdivRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m32）
    const divisor = this.readDwordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFFFFFn;
    // 获取被除数（EAX:EDX）
    const dividendLow = this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn;
    const dividendHigh = this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFFn;

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 将无符号bigint转换为有符号进行除法操作
    // 对于有符号除法，EDX是EAX的符号扩展
    const signedDividendLow = BigInt.asIntN(32, dividendLow);
    const signedDivisor = BigInt.asIntN(32, divisor);

    // 构建64位有符号被除数（通过符号扩展）
    const signBit = signedDividendLow >> 31n;
    const signedDividendHigh = BigInt.asIntN(32, signBit);
    const signedDividend = BigInt.asIntN(64, (signedDividendHigh << 32n) | BigInt.asUintN(32, signedDividendLow));

    // 执行有符号除法
    const signedQuotient = signedDividend / signedDivisor;
    const signedRemainder = signedDividend % signedDivisor;

    // 转换回无符号bigint
    const quotient = BigInt.asUintN(32, signedQuotient);
    const remainder = BigInt.asUintN(32, signedRemainder);

    // 检查商是否溢出（如果商超出了32位有符号整数的范围）
    if (signedQuotient < BigInt.asIntN(32, -0x80000000n) ||
      signedQuotient > BigInt.asIntN(32, 0x7FFFFFFFn)) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到EAX（商）和EDX（余数）寄存器（作为RAX和RDX的低32位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFF00000000n) | quotient);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFF00000000n) | remainder);

    // 除法指令不更新任何标志位
  }

  // IDIV r/m16 - 有符号除法指令（16位变体）
  private executeIdivRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m16）
    const divisor = this.readWordFromMemory(this.calculateEffectiveAddress(instruction)) & 0xFFFFn;
    // 获取被除数（AX:DX）
    const dividendLow = this.registerManager.getRegister(Registers.RAX) & 0xFFFFn;
    const dividendHigh = this.registerManager.getRegister(Registers.RDX) & 0xFFFFn;

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 将无符号bigint转换为有符号进行除法操作
    // 对于有符号除法，DX是AX的符号扩展
    const signedDividendLow = BigInt.asIntN(16, dividendLow);
    const signedDivisor = BigInt.asIntN(16, divisor);

    // 构建32位有符号被除数（通过符号扩展）
    const signBit = signedDividendLow >> 15n;
    const signedDividendHigh = BigInt.asIntN(16, signBit);
    const signedDividend = BigInt.asIntN(32, (signedDividendHigh << 16n) | BigInt.asUintN(16, signedDividendLow));

    // 执行有符号除法
    const signedQuotient = signedDividend / signedDivisor;
    const signedRemainder = signedDividend % signedDivisor;

    // 转换回无符号bigint
    const quotient = BigInt.asUintN(16, signedQuotient);
    const remainder = BigInt.asUintN(16, signedRemainder);

    // 检查商是否溢出（如果商超出了16位有符号整数的范围）
    if (signedQuotient < BigInt.asIntN(16, -0x8000n) ||
      signedQuotient > BigInt.asIntN(16, 0x7FFFn)) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到AX（商）和DX（余数）寄存器（作为RAX和RDX的低16位）
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | quotient);
    this.registerManager.setRegister(Registers.RDX, (this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFFFFFF0000n) | remainder);

    // 除法指令不更新任何标志位
  }

  // IDIV r/m8 - 有符号除法指令（8位变体）
  private executeIdivRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（除数，r/m8）
    const divisor = BigInt(this.readByteFromMemory(this.calculateEffectiveAddress(instruction))) & 0xFFn;
    // 获取被除数（AL:AH）
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const ahValue = (this.registerManager.getRegister(Registers.RAX) >> 8n) & 0xFFn;

    // 检查除数是否为0
    if (divisor === 0n) {
      // 触发除零错误
      this.raiseDivideError();
      return;
    }

    // 将无符号bigint转换为有符号进行除法操作
    // 对于有符号除法，AH是AL的符号扩展
    const signedAl = BigInt.asIntN(8, alValue);
    const signedDivisor = BigInt.asIntN(8, divisor);

    // 构建16位有符号被除数（通过符号扩展）
    const signBit = signedAl >> 7n;
    const signedAh = BigInt.asIntN(8, signBit);
    const signedDividend = BigInt.asIntN(16, (signedAh << 8n) | BigInt.asUintN(8, signedAl));

    // 执行有符号除法
    const signedQuotient = signedDividend / signedDivisor;
    const signedRemainder = signedDividend % signedDivisor;

    // 转换回无符号bigint
    const quotient = BigInt.asUintN(8, signedQuotient);
    const remainder = BigInt.asUintN(8, signedRemainder);

    // 检查商是否溢出（如果商超出了8位有符号整数的范围）
    if (signedQuotient < BigInt.asIntN(8, -0x80n) ||
      signedQuotient > BigInt.asIntN(8, 0x7Fn)) {
      this.raiseDivideError();
      return;
    }

    // 写回结果到AL（商）和AH（余数）寄存器（作为RAX的低16位）
    const axResult = (remainder << 8n) | quotient;
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFF0000n) | axResult);

    // 除法指令不更新任何标志位
  }

  // 触发除零错误
  private raiseDivideError(): void {
    // 在实际的模拟器中，这里应该触发中断或异常
    // 为了简化实现，我们这里只打印错误信息
    console.error("Divide by zero error or quotient overflow");
    // 在实际应用中，可能需要中止执行或跳转到异常处理程序
  }

  // TEST r/m64, r64 - 逻辑测试指令
  private executeTestRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取目标值
    let destValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行逻辑与操作但不保存结果，只用于设置标志位
    const result = destValue & srcValue;

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // TEST r64, r/m64 - 逻辑测试指令
  private executeTestRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 这个指令实际上与executeTestRmR的功能相同
    // 因为TEST指令的结果不依赖于操作数的顺序
    this.executeTestRmR(instruction);
  }

  // NOT r/m64 - 按位非指令（64位变体）
  private executeNot(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    const value = this.readQwordFromMemory(effectiveAddress);

    // 执行按位非操作
    const result = ~value;

    // 写回结果
    this.writeQwordToMemory(effectiveAddress, result);

    // NOT指令不影响任何标志位
  }

  // NOT r/m8 - 按位非指令（8位变体）
  private executeNot8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    // 读取8位值
    const value = BigInt(this.readByteFromMemory(effectiveAddress));

    // 执行按位非操作并保持在8位范围内
    const result = (~value) & 0xFFn;

    // 写回8位结果
    this.writeByteToMemory(effectiveAddress, Number(result));

    // NOT指令不影响任何标志位
  }

  // NOT r/m16 - 按位非指令（16位变体）
  private executeNot16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    // 读取16位值
    const value = this.readWordFromMemory(effectiveAddress);

    // 执行按位非操作并保持在16位范围内
    const result = (~value) & 0xFFFFn;

    // 写回16位结果
    this.writeWordToMemory(effectiveAddress, Number(result));

    // NOT指令不影响任何标志位
  }

  // NOT r/m32 - 按位非指令（32位变体）
  private executeNot32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    // 读取32位值
    const value = this.readDwordFromMemory(effectiveAddress);

    // 执行按位非操作并保持在32位范围内
    const result = (~value) & 0xFFFFFFFFn;

    // 写回32位结果
    this.writeDwordToMemory(effectiveAddress, result);

    // NOT指令不影响任何标志位
  }

  // NEG r/m64 - 求补指令 (计算 0 - 操作数)（64位变体）
  private executeNeg(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    const value = this.readQwordFromMemory(effectiveAddress);

    // 执行求补操作 (0 - value)
    const result = 0n - value;

    // 写回结果
    this.writeQwordToMemory(effectiveAddress, result);

    // 更新标志位，类似于减法操作
    // NEG指令的标志位更新与SUB 0, r/m指令相同
    this.updateFlagsForSub(value, 0n, result);

    // 对于NEG指令，CF标志在操作数非零时设置为1
    this.registerManager.setFlag('CF', value !== 0n);
  }

  // NEG r/m8 - 求补指令 (计算 0 - 操作数)（8位变体）
  private executeNeg8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    // 读取8位值
    const value = BigInt(this.readByteFromMemory(effectiveAddress));

    // 执行求补操作 (0 - value) 并保持在8位范围内
    const result = (0n - value) & 0xFFn;

    // 写回8位结果
    this.writeByteToMemory(effectiveAddress, Number(result));

    // 更新标志位，类似于减法操作
    // NEG指令的标志位更新与SUB 0, r/m指令相同
    this.updateFlagsForSub(value & 0xFFn, 0n, result);

    // 对于NEG指令，CF标志在操作数非零时设置为1
    this.registerManager.setFlag('CF', value !== 0n);
  }

  // NEG r/m16 - 求补指令 (计算 0 - 操作数)（16位变体）
  private executeNeg16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    // 读取16位值
    const value = this.readWordFromMemory(effectiveAddress);

    // 执行求补操作 (0 - value) 并保持在16位范围内
    const result = (0n - value) & 0xFFFFn;

    // 写回16位结果
    this.writeWordToMemory(effectiveAddress, Number(result));

    // 更新标志位，类似于减法操作
    // NEG指令的标志位更新与SUB 0, r/m指令相同
    this.updateFlagsForSub(value & 0xFFFFn, 0n, result);

    // 对于NEG指令，CF标志在操作数非零时设置为1
    this.registerManager.setFlag('CF', value !== 0n);
  }

  // NEG r/m32 - 求补指令 (计算 0 - 操作数)（32位变体）
  private executeNeg32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const effectiveAddress = this.calculateEffectiveAddress(instruction);
    // 读取32位值
    const value = this.readDwordFromMemory(effectiveAddress);

    // 执行求补操作 (0 - value) 并保持在32位范围内
    const result = (0n - value) & 0xFFFFFFFFn;

    // 写回32位结果
    this.writeDwordToMemory(effectiveAddress, result);

    // 更新标志位，类似于减法操作
    // NEG指令的标志位更新与SUB 0, r/m指令相同
    this.updateFlagsForSub(value & 0xFFFFFFFFn, 0n, result);

    // 对于NEG指令，CF标志在操作数非零时设置为1
    this.registerManager.setFlag('CF', value !== 0n);
  }

  // TEST r/m64, imm8/32/64 - 逻辑测试指令（带立即数）
  private executeTestImm(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取目标值
    let destValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 获取立即数
    const immediate = instruction.immediate;

    // 执行逻辑与操作但不保存结果，只用于设置标志位
    const result = destValue & immediate;

    // 更新标志位
    this.updateFlagsForLogic(destValue, immediate, result);
  }

  // IMUL r64, r/m64, imm8/32 - 有符号乘法指令（三操作数形式）
  private executeImulRRImm(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取目标寄存器
    const destReg = instruction.modrm.reg;
    // 获取源操作数（r/m64）
    let srcValue: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      const memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 获取立即数
    const immediate = instruction.immediate;

    // 执行64位有符号乘法
    const signedSrc = BigInt.asIntN(64, srcValue);
    const signedImm = BigInt.asIntN(64, immediate);
    const signedResult = signedSrc * signedImm;
    const result = BigInt.asUintN(64, signedResult);

    // 写回结果到目标寄存器
    this.registerManager.setRegister(destReg, result);

    // 更新标志位
    // CF和OF在IMUL指令中设置为1如果结果需要超过64位来表示，否则设置为0
    this.registerManager.setFlag('CF', signedResult !== BigInt.asIntN(64, signedResult));
    this.registerManager.setFlag('OF', signedResult !== BigInt.asIntN(64, signedResult));
    // ZF、SF、AF和PF未定义，保持不变
  }

  // CMP r64, r/m64 指令实现（64位变体）
  private executeCmpRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值
    const destValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // CMP r8, r/m8 指令实现（8位变体）
  private executeCmpRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值（只取低8位）
    const destRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const destValue = destRegisterValue & 0xFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      const srcRegisterValue = this.registerManager.getRegister(instruction.modrm.rm);
      srcValue = srcRegisterValue & 0xFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位（保持8位操作的语义）
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // CMP r16, r/m16 指令实现（16位变体）
  private executeCmpRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值（只取低16位）
    const destRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const destValue = destRegisterValue & 0xFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      const srcRegisterValue = this.registerManager.getRegister(instruction.modrm.rm);
      srcValue = srcRegisterValue & 0xFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readWordFromMemory(memoryAddress);
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位（保持16位操作的语义）
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // CMP r32, r/m32 指令实现（32位变体）
  private executeCmpRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值（只取低32位）
    const destRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const destValue = destRegisterValue & 0xFFFFFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      const srcRegisterValue = this.registerManager.getRegister(instruction.modrm.rm);
      srcValue = srcRegisterValue & 0xFFFFFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readDwordFromMemory(memoryAddress);
    }

    // 执行比较操作（实际执行减法但不保存结果）
    const result = destValue - srcValue;

    // 更新标志位（保持32位操作的语义）
    this.updateFlagsForSub(destValue, srcValue, result);
  }

  // SHL r/m64, imm8指令
  private executeShlImm(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    const count = Number(instruction.immediate) & 0x3F; // x86-64中移位计数只使用低6位
    const addr = this.calculateEffectiveAddress(instruction);
    let value = this.readQwordFromMemory(addr);

    if (count > 0) {
      // 保存移位前的最高位用于CF计算
      const highestBit = (value >> 63n) & 1n;
      // 执行左移操作
      value <<= BigInt(count);
      this.writeQwordToMemory(addr, value);

      // 更新标志位
      this.updateFlagsForShift(value, highestBit, count);
    }
  }

  // SHR r/m64, imm8指令
  private executeShrImm(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    const count = Number(instruction.immediate) & 0x3F;
    const addr = this.calculateEffectiveAddress(instruction);
    let value = this.readQwordFromMemory(addr);

    if (count > 0) {
      // 保存移位前的最低位用于CF计算
      const lowestBit = value & 1n;
      // 执行无符号右移操作
      value = value >> BigInt(count);
      this.writeQwordToMemory(addr, value);

      // 更新标志位
      this.updateFlagsForShift(value, lowestBit, count);
    }
  }

  // SHLD r/m64, r/m64, imm8 - 双精度左移指令（立即数）
  private executeShldImm(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取目标操作数（r/m64）
    let destinationValue: u64;
    let isDestinationRegister = false;
    let destinationRegisterIndex: Registers | undefined;
    let destinationMemoryAddress: u64 | undefined;

    if (instruction.modrm.mod === 3) {
      destinationValue = this.registerManager.getRegister(instruction.modrm.rm);
      isDestinationRegister = true;
      destinationRegisterIndex = instruction.modrm.rm;
    } else {
      destinationMemoryAddress = this.calculateEffectiveAddress(instruction);
      destinationValue = this.readQwordFromMemory(destinationMemoryAddress);
    }

    // 获取源操作数（寄存器）
    const sourceRegisterIndex = instruction.modrm.reg;
    const sourceValue = this.registerManager.getRegister(sourceRegisterIndex);

    // 移位计数（限制为64位内）
    const count = Number(instruction.immediate) & 0x3F;
    if (count === 0) return;

    // 执行双精度左移操作
    // z = x << b | y >> (64 - b)
    // 其中x是目标操作数，y是源操作数，b是移位计数
    const result = (destinationValue << BigInt(count)) | (sourceValue >> BigInt(64 - count));

    // 保存移位前被移出的位用于CF计算
    const carryBit = (destinationValue >> BigInt(64 - count)) & 1n;

    // 写回结果
    if (isDestinationRegister && destinationRegisterIndex !== undefined) {
      this.registerManager.setRegister(destinationRegisterIndex, result);
    } else if (destinationMemoryAddress !== undefined) {
      this.writeQwordToMemory(destinationMemoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForDoubleShift(result, carryBit, count, destinationValue);
  }

  // SHRD r/m64, r/m64, imm8 - 双精度右移指令（立即数）
  private executeShrdImm(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取目标操作数（r/m64）
    let destinationValue: u64;
    let isDestinationRegister = false;
    let destinationRegisterIndex: Registers | undefined;
    let destinationMemoryAddress: u64 | undefined;

    if (instruction.modrm.mod === 3) {
      destinationValue = this.registerManager.getRegister(instruction.modrm.rm);
      isDestinationRegister = true;
      destinationRegisterIndex = instruction.modrm.rm;
    } else {
      destinationMemoryAddress = this.calculateEffectiveAddress(instruction);
      destinationValue = this.readQwordFromMemory(destinationMemoryAddress);
    }

    // 获取源操作数（寄存器）
    const sourceRegisterIndex = instruction.modrm.reg;
    const sourceValue = this.registerManager.getRegister(sourceRegisterIndex);

    // 移位计数（限制为64位内）
    const count = Number(instruction.immediate) & 0x3F;
    if (count === 0) return;

    // 执行双精度右移操作
    // z = x >> b | y << (64 - b)
    // 其中x是目标操作数，y是源操作数，b是移位计数
    const result = (destinationValue >> BigInt(count)) | (sourceValue << BigInt(64 - count));

    // 保存移位前被移出的位用于CF计算
    const carryBit = (destinationValue >> BigInt(count - 1)) & 1n;

    // 写回结果
    if (isDestinationRegister && destinationRegisterIndex !== undefined) {
      this.registerManager.setRegister(destinationRegisterIndex, result);
    } else if (destinationMemoryAddress !== undefined) {
      this.writeQwordToMemory(destinationMemoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForDoubleShift(result, carryBit, count, destinationValue);
  }

  // SHLD r/m64, r/m64, CL - 双精度左移指令（CL寄存器）
  private executeShldCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标操作数（r/m64）
    let destinationValue: u64;
    let isDestinationRegister = false;
    let destinationRegisterIndex: Registers | undefined;
    let destinationMemoryAddress: u64 | undefined;

    if (instruction.modrm.mod === 3) {
      destinationValue = this.registerManager.getRegister(instruction.modrm.rm);
      isDestinationRegister = true;
      destinationRegisterIndex = instruction.modrm.rm;
    } else {
      destinationMemoryAddress = this.calculateEffectiveAddress(instruction);
      destinationValue = this.readQwordFromMemory(destinationMemoryAddress);
    }

    // 获取源操作数（寄存器）
    const sourceRegisterIndex = instruction.modrm.reg;
    const sourceValue = this.registerManager.getRegister(sourceRegisterIndex);

    // 从CL寄存器获取移位计数（CL是RCX的低8位）
    const rcxValue = this.registerManager.getRegister(Registers.RCX);
    const clValue = Number(rcxValue & 0xFFn) & 0x3F;
    if (clValue === 0) return;

    // 执行双精度左移操作
    const result = (destinationValue << BigInt(clValue)) | (sourceValue >> BigInt(64 - clValue));

    // 保存移位前被移出的位用于CF计算
    const carryBit = (destinationValue >> BigInt(64 - clValue)) & 1n;

    // 写回结果
    if (isDestinationRegister && destinationRegisterIndex !== undefined) {
      this.registerManager.setRegister(destinationRegisterIndex, result);
    } else if (destinationMemoryAddress !== undefined) {
      this.writeQwordToMemory(destinationMemoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForDoubleShift(result, carryBit, clValue, destinationValue);
  }

  // SHRD r/m64, r/m64, CL - 双精度右移指令（CL寄存器）
  private executeShrdCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标操作数（r/m64）
    let destinationValue: u64;
    let isDestinationRegister = false;
    let destinationRegisterIndex: Registers | undefined;
    let destinationMemoryAddress: u64 | undefined;

    if (instruction.modrm.mod === 3) {
      destinationValue = this.registerManager.getRegister(instruction.modrm.rm);
      isDestinationRegister = true;
      destinationRegisterIndex = instruction.modrm.rm;
    } else {
      destinationMemoryAddress = this.calculateEffectiveAddress(instruction);
      destinationValue = this.readQwordFromMemory(destinationMemoryAddress);
    }

    // 获取源操作数（寄存器）
    const sourceRegisterIndex = instruction.modrm.reg;
    const sourceValue = this.registerManager.getRegister(sourceRegisterIndex);

    // 从CL寄存器获取移位计数（CL是RCX的低8位）
    const rcxValue = this.registerManager.getRegister(Registers.RCX);
    const clValue = Number(rcxValue & 0xFFn) & 0x3F;
    if (clValue === 0) return;

    // 执行双精度右移操作
    const result = (destinationValue >> BigInt(clValue)) | (sourceValue << BigInt(64 - clValue));

    // 保存移位前被移出的位用于CF计算
    const carryBit = (destinationValue >> BigInt(clValue - 1)) & 1n;

    // 写回结果
    if (isDestinationRegister && destinationRegisterIndex !== undefined) {
      this.registerManager.setRegister(destinationRegisterIndex, result);
    } else if (destinationMemoryAddress !== undefined) {
      this.writeQwordToMemory(destinationMemoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForDoubleShift(result, carryBit, clValue, destinationValue);
  }

  // 更新双精度移位指令的标志位
  private updateFlagsForDoubleShift(result: u64, carryBit: u64, count: number, originalValue: u64): void {
    // CF标志设置为最后被移出的位
    this.registerManager.setFlag('CF', carryBit !== 0n);

    // OF标志仅在移位1位时定义
    if (count === 1) {
      // OF = 结果的符号位 != 原始值的符号位
      const oldSignBit = (originalValue >> 63n) & 1n;
      const newSignBit = (result >> 63n) & 1n;
      this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
    } else if (count > 1) {
      // 移位计数大于1时，OF标志未定义（保持不变）
    }

    // ZF标志根据结果是否为零设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF标志设置为结果的最高位
    this.registerManager.setFlag('SF', Number(result >> 63n) !== 0);

    // PF标志设置为结果的奇偶性
    const bitCount = this.countSetBits(result & 0xFFn);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // AF标志未定义
  }

  /**
   * 执行BSF指令 - 位扫描正向
   * 功能：在源操作数中从最低位（LSB）开始扫描，找到第一个设置为1的位
   * 如果源操作数为0，则ZF标志设置为1，目标操作数未定义
   * 否则，ZF标志设置为0，目标操作数设置为找到的位位置（从0开始计数）
   * @param instruction 解码后的指令
   */
  private executeBsf(instruction: DecodedInstruction): void {
    if (!instruction.modrm) {
      // 应该不会发生，因为BSF指令需要ModR/M字节
      return;
    }

    // 获取源操作数（r/m64）
    const sourceValue = this.readQwordFromMemory(this.calculateEffectiveAddress(instruction));
    // 获取目标寄存器索引
    const destRegister = instruction.modrm.reg;

    if (sourceValue === 0n) {
      // 如果源操作数为0，设置ZF标志为1
      this.registerManager.setFlag('ZF', true);
      // 目标操作数未定义（保持不变）
    } else {
      // 如果源操作数不为0，找到最低位的1的位置
      let bitPosition = 0;
      let temp = sourceValue;

      // 使用与blink类似的算法实现BSF
      // 将最低位的1提取出来
      temp &= -temp;

      // 计算最低位1的位置
      if (temp & 0xFFFFFFFFn) {
        if (temp & 0xFFFFn) {
          if (temp & 0xFFn) {
            if (temp & 0x0Fn) {
              if (temp & 0x03n) {
                if (temp & 0x01n) bitPosition = 0;
                else bitPosition = 1;
              } else {
                if (temp & 0x04n) bitPosition = 2;
                else bitPosition = 3;
              }
            } else {
              if (temp & 0x30n) {
                if (temp & 0x10n) bitPosition = 4;
                else bitPosition = 5;
              } else {
                if (temp & 0x40n) bitPosition = 6;
                else bitPosition = 7;
              }
            }
          } else {
            bitPosition = 8 + this.countSetBits(temp >> 8n) - 1;
          }
        } else {
          bitPosition = 16 + this.countSetBits(temp >> 16n) - 1;
        }
      } else {
        bitPosition = 32 + this.countSetBits(temp >> 32n) - 1;
      }

      // 将结果写入目标寄存器
      this.registerManager.setRegister(destRegister, BigInt(bitPosition));
      // 清除ZF标志
      this.registerManager.setFlag('ZF', false);
    }
  }

  /**
   * 执行位操作指令（BT/BTS/BTR/BTC）
   * BT: 测试指定位的值，并将其复制到CF标志
   * BTS: 测试指定位的值，复制到CF标志，然后将该位置为1
   * BTR: 测试指定位的值，复制到CF标志，然后将该位复位为0
   * BTC: 测试指定位的值，复制到CF标志，然后将该位取反
   * @param instruction 解码后的指令
   */
  private executeBitOp(instruction: DecodedInstruction): void {
    if (!instruction.modrm) {
      // 应该不会发生，因为这些指令需要ModR/M字节
      return;
    }

    let bitPosition: number;
    let bitOffset: bigint = 0n;
    let bitMask: bigint;

    // 根据指令类型确定位位置的来源
    if (instruction.opcode === 0xBA) {
      // BT r/m64, imm8 - 立即数位操作
      if (instruction.immediate === undefined) {
        return;
      }
      bitPosition = Number(instruction.immediate) & 0x3F; // 64位中的位位置
    } else {
      // 寄存器位操作
      const bitRegValue = this.registerManager.getRegister(instruction.modrm.reg);
      bitPosition = Number(bitRegValue & 0x3Fn); // 64位中的位位置
      bitOffset = bitRegValue >> 6n; // 字节偏移量（超过64位的部分）
    }

    // 计算要操作的位掩码
    bitMask = 1n << BigInt(bitPosition);

    // 计算有效地址
    let effectiveAddress = this.calculateEffectiveAddress(instruction);
    if (bitOffset !== 0n) {
      // 如果有字节偏移量，调整有效地址
      effectiveAddress += bitOffset;
    }

    // 读取源操作数
    let sourceValue = this.readQwordFromMemory(effectiveAddress);

    // 获取指定位的值并设置CF标志
    const bitValue = (sourceValue & bitMask) !== 0n;
    this.registerManager.setFlag('CF', bitValue);

    // 根据不同的指令类型执行不同的操作
    switch (instruction.opcode) {
      case 0x0FA3: // BT - 仅测试位，不修改源操作数
      case 0xBA:  // BT imm8 - 仅测试位，不修改源操作数
        // 不需要修改源操作数
        break;
      case 0x0FAD: // BTS - 测试位并置位
        sourceValue |= bitMask;
        break;
      case 0x0FAB: // BTR - 测试位并复位
        sourceValue &= ~bitMask;
        break;
      case 0x0FB3: // BTC - 测试位并取反
        sourceValue ^= bitMask;
        break;
    }

    // 如果不是BT指令，写回修改后的值
    if (instruction.opcode !== 0x0FA3 && instruction.opcode !== 0xBA) {
      this.writeQwordToMemory(effectiveAddress, sourceValue);
    }
  }

  /**
   * 执行XADD指令 - 交换并相加
   * 功能：交换两个操作数的值，然后将它们的和存储在第一个操作数中
   * XADD指令会更新CF、OF、SF、ZF、AF和PF标志
   * @param instruction 解码后的指令
   */
  private executeXadd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数（r64）
    const sourceRegister = instruction.modrm.reg;
    const sourceValue = this.registerManager.getRegister(sourceRegister);

    // 获取目标操作数
    let destValue: u64;
    let destRegister: Registers | undefined;
    let memoryAddress: u64 | undefined;

    if (instruction.modrm.mod === 3) {
      // 如果是寄存器操作数
      destRegister = instruction.modrm.rm;
      destValue = this.registerManager.getRegister(destRegister);
    } else {
      // 如果是内存操作数
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 计算两个操作数的和
    const sum = sourceValue + destValue;

    // 根据原始blink实现中的标志位更新逻辑来设置标志
    this.updateFlagsForAdd(sourceValue, destValue, sum);

    // 交换操作数的值 - 将源操作数的值存储到目标操作数
    if (instruction.modrm.mod === 3 && destRegister !== undefined) {
      this.registerManager.setRegister(destRegister, sourceValue);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, sourceValue);
    }

    // 将和存储到源操作数
    this.registerManager.setRegister(sourceRegister, sum);
  }

  /**
   * 执行CMPXCHG指令 - 比较并交换
   * 功能：比较累加器（RAX）的值与目标操作数的值
   * 如果它们相等，将源操作数的值复制到目标操作数，并清除ZF标志
   * 如果它们不相等，将目标操作数的值复制到累加器，并设置ZF标志
   * CMPXCHG指令会更新CF、OF、SF、ZF、AF和PF标志
   * @param instruction 解码后的指令
   */
  private executeCmpxchg(instruction: DecodedInstruction): void {
    if (!instruction.modrm) {
      return;
    }

    // 获取源操作数（r64）
    const sourceRegister = instruction.modrm.reg;
    const sourceValue = this.registerManager.getRegister(sourceRegister);

    // 获取目标操作数（r/m64）
    const destinationValue = this.readQwordFromMemory(this.calculateEffectiveAddress(instruction));

    // 获取累加器（RAX）的值
    const raxValue = this.registerManager.getRegister(0); // 0是RAX寄存器的索引

    // 比较RAX和目标操作数
    const difference = raxValue - destinationValue;

    // 根据差值设置标志位
    this.updateFlagsForSub(raxValue, destinationValue, difference);

    // 如果RAX等于目标操作数，执行交换
    if (raxValue === destinationValue) {
      // 清除ZF标志
      this.registerManager.setFlag('ZF', false);

      // 将源操作数写入目标位置
      if (instruction.modrm.mod === 3) {
        // 如果是寄存器操作数，直接写入
        this.registerManager.setRegister(instruction.modrm.rm, sourceValue);
      } else {
        // 如果是内存操作数，写入内存
        const address = this.calculateEffectiveAddress(instruction);
        this.writeQwordToMemory(address, sourceValue);
      }
    } else {
      // 如果不相等，将目标操作数的值复制到RAX
      this.registerManager.setRegister(0, destinationValue);

      // 设置ZF标志
      this.registerManager.setFlag('ZF', true);
    }
  }

  /**
   * 执行BSR指令 - 位扫描反向
   * 功能：在源操作数中从最高位（MSB）开始扫描，找到第一个设置为1的位
   * 如果源操作数为0，则ZF标志设置为1，目标操作数未定义
   * 否则，ZF标志设置为0，目标操作数设置为找到的位位置（从0开始计数）
   * @param instruction 解码后的指令
   */
  private executeBsr(instruction: DecodedInstruction): void {
    if (!instruction.modrm) {
      // 应该不会发生，因为BSR指令需要ModR/M字节
      return;
    }

    // 获取源操作数（r/m64）
    const sourceValue = this.readQwordFromMemory(this.calculateEffectiveAddress(instruction));
    // 获取目标寄存器索引
    const destRegister = instruction.modrm.reg;

    if (sourceValue === 0n) {
      // 如果源操作数为0，设置ZF标志为1
      this.registerManager.setFlag('ZF', true);
      // 目标操作数未定义（保持不变）
    } else {
      // 如果源操作数不为0，找到最高位的1的位置
      let bitPosition = 63;
      let temp = sourceValue;

      // 使用与blink类似的算法实现BSR
      // 展开所有低位以简化最高位检测
      temp |= temp >> 1n;
      temp |= temp >> 2n;
      temp |= temp >> 4n;
      temp |= temp >> 8n;
      temp |= temp >> 16n;
      temp |= temp >> 32n;

      // 使用De Bruijn序列算法快速计算最高位1的位置
      // 这个实现基于blink中的bsr函数
      const debruijnSequence = 0x03f79d71b4cb0a89n;
      const debruijnTable = [
        0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61,
        54, 58, 35, 52, 50, 42, 21, 44, 38, 32, 29, 23, 17, 11, 4, 62,
        46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43, 31, 22, 10, 45,
        25, 39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63
      ];

      bitPosition = debruijnTable[Number(((temp * debruijnSequence) >> 58n) & 0x3fn)];

      // 将结果写入目标寄存器
      this.registerManager.setRegister(destRegister, BigInt(bitPosition));
      // 清除ZF标志
      this.registerManager.setFlag('ZF', false);
    }
  }

  // 计算有效地址
  private calculateEffectiveAddress(instruction: DecodedInstruction): u64 {
    if (!instruction.modrm) return 0n;

    let address: u64 = 0n;

    // 获取基址寄存器值
    if (instruction.modrm.mod !== 3 && instruction.modrm.rm < 6) {
      address = this.registerManager.getRegister(instruction.modrm.rm);
    }

    // 添加偏移量
    if (instruction.displacement !== undefined) {
      address += instruction.displacement;
    }

    return address;
  }

  // 从内存读取64位值
  private readQwordFromMemory(address: u64): u64 {
    const bytes = this.memoryManager.read(address, 8);
    // 小端序转换
    return BigInt(bytes[0]) |
      (BigInt(bytes[1]) << 8n) |
      (BigInt(bytes[2]) << 16n) |
      (BigInt(bytes[3]) << 24n) |
      (BigInt(bytes[4]) << 32n) |
      (BigInt(bytes[5]) << 40n) |
      (BigInt(bytes[6]) << 48n) |
      (BigInt(bytes[7]) << 56n);
  }

  // 向内存写入64位值
  private writeQwordToMemory(address: u64, value: u64): void {
    const bytes = new Uint8Array(8);
    // 小端序转换
    bytes[0] = Number(value & 0xFFn);
    bytes[1] = Number((value >> 8n) & 0xFFn);
    bytes[2] = Number((value >> 16n) & 0xFFn);
    bytes[3] = Number((value >> 24n) & 0xFFn);
    bytes[4] = Number((value >> 32n) & 0xFFn);
    bytes[5] = Number((value >> 40n) & 0xFFn);
    bytes[6] = Number((value >> 48n) & 0xFFn);
    bytes[7] = Number((value >> 56n) & 0xFFn);

    this.memoryManager.write(address, bytes);
  }

  // 从内存读取8位值
  private readByteFromMemory(address: u64): number {
    const bytes = this.memoryManager.read(address, 1);
    return bytes[0];
  }

  // 向内存写入8位值
  private writeByteToMemory(address: u64, value: number): void {
    const bytes = new Uint8Array(1);
    bytes[0] = value;
    this.memoryManager.write(address, bytes);
  }

  // 向内存写入16位值
  private writeWordToMemory(address: u64, value: number): void {
    const bytes = new Uint8Array(2);
    // 小端序转换
    bytes[0] = value & 0xFF;
    bytes[1] = (value >> 8) & 0xFF;

    this.memoryManager.write(address, bytes);
  }

  // 向内存写入32位值
  private writeDwordToMemory(address: u64, value: u64): void {
    const bytes = new Uint8Array(4);
    // 小端序转换
    bytes[0] = Number(value & 0xFFn);
    bytes[1] = Number((value >> 8n) & 0xFFn);
    bytes[2] = Number((value >> 16n) & 0xFFn);
    bytes[3] = Number((value >> 24n) & 0xFFn);

    this.memoryManager.write(address, bytes);
  }

  // 从内存读取16位值
  private readWordFromMemory(address: u64): u64 {
    const bytes = this.memoryManager.read(address, 2);
    // 小端序转换
    return BigInt(bytes[0]) | (BigInt(bytes[1]) << 8n);
  }

  // 从内存读取32位值
  private readDwordFromMemory(address: u64): u64 {
    const bytes = this.memoryManager.read(address, 4);
    // 小端序转换
    return BigInt(bytes[0]) |
      (BigInt(bytes[1]) << 8n) |
      (BigInt(bytes[2]) << 16n) |
      (BigInt(bytes[3]) << 24n);
  }

  // CLC - 清除进位标志
  private executeClc(instruction: DecodedInstruction): void {
    this.registerManager.setFlag('CF', false);
  }

  // STC - 设置进位标志
  private executeStc(instruction: DecodedInstruction): void {
    this.registerManager.setFlag('CF', true);
  }

  // CMC - 翻转进位标志
  private executeCmc(instruction: DecodedInstruction): void {
    const currentCF = this.registerManager.getFlag('CF');
    this.registerManager.setFlag('CF', !currentCF);
  }

  // CLI - 清除中断标志
  private executeCli(instruction: DecodedInstruction): void {
    this.registerManager.setFlag('IF', false);
  }

  // STI - 设置中断标志
  private executeSti(instruction: DecodedInstruction): void {
    this.registerManager.setFlag('IF', true);
  }

  // CLD - 清除方向标志
  private executeCld(instruction: DecodedInstruction): void {
    this.registerManager.setFlag('DF', false);
  }

  // STD - 设置方向标志
  private executeStd(instruction: DecodedInstruction): void {
    this.registerManager.setFlag('DF', true);
  }

  // JNZ/JNE - 不为零/不等于时跳转
  private executeJnz(immediate: u64): void {
    const zf = this.registerManager.getFlag('ZF');
    if (!zf) {
      // 不为零时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JLE - 小于等于时跳转
  private executeJle(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const sf = this.registerManager.getFlag('SF');
    const of = this.registerManager.getFlag('OF');
    const zf = this.registerManager.getFlag('ZF');

    if (zf || (sf !== of)) {
      // 小于等于时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // JG - 大于时跳转
  private executeJg(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const sf = this.registerManager.getFlag('SF');
    const of = this.registerManager.getFlag('OF');
    const zf = this.registerManager.getFlag('ZF');

    if (!zf && (sf === of)) {
      // 大于时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // JGE - 大于等于时跳转
  private executeJge(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const sf = this.registerManager.getFlag('SF');
    const of = this.registerManager.getFlag('OF');

    if (sf === of) {
      // 大于等于时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // JL - 小于时跳转
  private executeJl(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const sf = this.registerManager.getFlag('SF');
    const of = this.registerManager.getFlag('OF');

    if (sf !== of) {
      // 小于时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // JBE - 小于等于时跳转
  private executeJbe(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const cf = this.registerManager.getFlag('CF');
    const zf = this.registerManager.getFlag('ZF');

    if (cf || zf) {
      // 小于等于时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // JA - 大于时跳转
  private executeJa(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const cf = this.registerManager.getFlag('CF');
    const zf = this.registerManager.getFlag('ZF');

    if (!cf && !zf) {
      // 大于时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // JMP - 相对跳转
  private executeJmpRelative(immediate: u64): void {
    const rip = this.registerManager.getInstructionPointer();
    this.registerManager.setInstructionPointer(rip + immediate);
  }

  // XCHG r/m64, reg64 - 交换寄存器和内存值
  private executeXchgRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const srcReg = instruction.modrm.reg;
    const srcValue = this.registerManager.getRegister(srcReg);
    const dstAddr = this.calculateEffectiveAddress(instruction);
    const dstValue = this.readQwordFromMemory(dstAddr);

    // 交换值
    this.registerManager.setRegister(srcReg, dstValue);
    this.writeQwordToMemory(dstAddr, srcValue);
  }

  // XCHG reg64, r/m64 - 交换寄存器和内存值
  private executeXchgRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const dstReg = instruction.modrm.reg;
    const dstValue = this.registerManager.getRegister(dstReg);
    const srcAddr = this.calculateEffectiveAddress(instruction);
    const srcValue = this.readQwordFromMemory(srcAddr);

    // 交换值
    this.registerManager.setRegister(dstReg, srcValue);
    this.writeQwordToMemory(srcAddr, dstValue);
  }

  // JMP reg/mem64 - 跳转到寄存器或内存指定的地址
  private executeJmpRM(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let targetAddress: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      targetAddress = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      const memoryAddress = this.calculateEffectiveAddress(instruction);
      targetAddress = this.readQwordFromMemory(memoryAddress);
    }

    // 直接设置指令指针
    this.registerManager.setInstructionPointer(targetAddress);
  }

  // JCXZ/JECXZ/JRCXZ - CX/ECX/RCX为零则跳转
  private executeJcxz(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    // 获取RCX寄存器的值（在不同模式下使用不同部分）
    const rcx = this.registerManager.getRegister(Registers.RCX);

    // 在x86架构中，JCXZ/JECXZ/JRCXZ指令的行为取决于当前运行模式
    // 16位模式检查CX (低16位)，32位模式检查ECX (低32位)，64位模式检查RCX
    // 由于我们主要关注64位模式，这里简化处理，直接检查整个RCX寄存器

    if (rcx === 0n) {
      // 如果RCX为零，则跳转到目标地址
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // JZ - 为零时跳转
  private executeJz(immediate: u64): void {
    const zf = this.registerManager.getFlag('ZF');
    if (zf) {
      // 为零时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JNO - 无溢出时跳转
  private executeJno(immediate: u64): void {
    const of = this.registerManager.getFlag('OF');
    if (!of) {
      // 无溢出时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JNP/JPO - 奇偶位为奇数时跳转
  private executeJnp(immediate: u64): void {
    const pf = this.registerManager.getFlag('PF');
    if (!pf) {
      // 奇偶位为奇数时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JB/JC - 小于/进位标志为1时跳转
  private executeJb(immediate: u64): void {
    const cf = this.registerManager.getFlag('CF');
    if (cf) {
      // 小于/进位标志为1时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JAE/JNC - 大于等于/进位标志为0时跳转
  private executeJae(immediate: u64): void {
    const cf = this.registerManager.getFlag('CF');
    if (!cf) {
      // 大于等于/进位标志为0时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JS - 符号标志为1时跳转
  private executeJs(immediate: u64): void {
    const sf = this.registerManager.getFlag('SF');
    if (sf) {
      // 符号标志为1时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JNS - 符号标志为0时跳转
  private executeJns(immediate: u64): void {
    const sf = this.registerManager.getFlag('SF');
    if (!sf) {
      // 符号标志为0时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JO - 溢出标志为1时跳转
  private executeJo(immediate: u64): void {
    const of = this.registerManager.getFlag('OF');
    if (of) {
      // 溢出标志为1时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // JP/JPE - 奇偶标志为1时跳转
  private executeJp(immediate: u64): void {
    const pf = this.registerManager.getFlag('PF');
    if (pf) {
      // 奇偶标志为1时跳转
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + immediate);
    }
  }

  // LEA - 加载有效地址指令
  private executeLea(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算有效地址
    const effectiveAddress = this.calculateEffectiveAddress(instruction);

    // 将有效地址存储到目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, effectiveAddress);
  }

  // CALL rel32 - 相对调用
  private executeCallRelative(immediate: u64): void {
    // 保存返回地址（下一条指令的地址）
    const rip = this.registerManager.getInstructionPointer();
    this.push(rip);

    // 跳转到目标地址
    this.registerManager.setInstructionPointer(rip + immediate);
  }

  // CALL r/m64 - 寄存器或内存间接调用
  private executeCallRM(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let targetAddress: u64;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      targetAddress = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      const memoryAddress = this.calculateEffectiveAddress(instruction);
      targetAddress = this.readQwordFromMemory(memoryAddress);
    }

    // 保存返回地址（下一条指令的地址）
    const rip = this.registerManager.getInstructionPointer();
    this.push(rip);

    // 跳转到目标地址
    this.registerManager.setInstructionPointer(targetAddress);
  }

  // RET - 返回指令
  private executeRet(): void {
    // 从栈中弹出返回地址并跳转
    const returnAddress = this.pop();
    this.registerManager.setInstructionPointer(returnAddress);
  }

  // RET imm16 - 带立即数返回指令
  private executeRetImm(immediate: u64): void {
    // 从栈中弹出返回地址并跳转
    const returnAddress = this.pop();
    this.registerManager.setInstructionPointer(returnAddress);

    // 调整栈指针（增加立即数的值）
    const rsp = this.registerManager.getRegister(Registers.RSP);
    this.registerManager.setRegister(Registers.RSP, rsp + immediate);
  }

  // OR r/m64, r64 指令实现 - 64位变体
  private executeOrRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行OR操作
    const result = destValue | srcValue;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // OR r/m8, r8 指令实现 - 8位变体
  private executeOrRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低8位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;
    let originalValue: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      originalValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = originalValue & 0xFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 执行OR操作并保持在8位范围内
    const result = (destValue | srcValue) & 0xFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined && originalValue !== undefined) {
      // 只更新寄存器的低8位
      const newValue = (originalValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeByteToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // OR r/m16, r16 指令实现 - 16位变体
  private executeOrRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低16位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;
    let originalValue: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      originalValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = originalValue & 0xFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readWordFromMemory(memoryAddress);
    }

    // 执行OR操作并保持在16位范围内
    const result = (destValue | srcValue) & 0xFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined && originalValue !== undefined) {
      // 只更新寄存器的低16位
      const newValue = (originalValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeWordToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // OR r/m32, r32 指令实现 - 32位变体
  private executeOrRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低32位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;
    let originalValue: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      originalValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = originalValue & 0xFFFFFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readDwordFromMemory(memoryAddress);
    }

    // 执行OR操作并保持在32位范围内
    const result = (destValue | srcValue) & 0xFFFFFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined && originalValue !== undefined) {
      // 只更新寄存器的低32位
      const newValue = (originalValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeDwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // OR r64, r/m64 指令实现 - 64位变体
  private executeOrRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值
    const destValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行OR操作
    const result = destValue | srcValue;

    // 写回结果到目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, result);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // OR r8, r/m8 指令实现 - 8位变体
  private executeOrRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低8位）
    const destValue = currentValue & 0xFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 执行OR操作并保持在8位范围内
    const result = (destValue | srcValue) & 0xFFn;

    // 只更新寄存器的低8位
    const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // OR r16, r/m16 指令实现 - 16位变体
  private executeOrRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低16位）
    const destValue = currentValue & 0xFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readWordFromMemory(memoryAddress);
    }

    // 执行OR操作并保持在16位范围内
    const result = (destValue | srcValue) & 0xFFFFn;

    // 只更新寄存器的低16位
    const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // OR r32, r/m32 指令实现 - 32位变体
  private executeOrRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    // 获取目标寄存器的当前值
    const currentValue = this.registerManager.getRegister(destReg);
    // 获取目标值（只取低32位）
    const destValue = currentValue & 0xFFFFFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readDwordFromMemory(memoryAddress);
    }

    // 执行OR操作并保持在32位范围内
    const result = (destValue | srcValue) & 0xFFFFFFFFn;

    // 只更新寄存器的低32位
    const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
    this.registerManager.setRegister(destReg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r/m64, r64 指令实现 - 64位变体
  private executeXorRmR(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      destValue = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行XOR操作
    const result = destValue ^ srcValue;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r/m8, r8 指令实现 - 8位变体
  private executeXorRmR8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低8位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;
    let originalValue: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      originalValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = originalValue & 0xFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 执行XOR操作并保持在8位范围内
    const result = (destValue ^ srcValue) & 0xFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined && originalValue !== undefined) {
      // 只更新寄存器的低8位
      const newValue = (originalValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeByteToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r/m16, r16 指令实现 - 16位变体
  private executeXorRmR16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低16位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;
    let originalValue: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      originalValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = originalValue & 0xFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readWordFromMemory(memoryAddress);
    }

    // 执行XOR操作并保持在16位范围内
    const result = (destValue ^ srcValue) & 0xFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined && originalValue !== undefined) {
      // 只更新寄存器的低16位
      const newValue = (originalValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeWordToMemory(memoryAddress, Number(result));
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r/m32, r32 指令实现 - 32位变体
  private executeXorRmR32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源寄存器值（只取低32位）
    const srcValue = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFFFFFn;
    // 获取目标值
    let destValue: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;
    let originalValue: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      originalValue = this.registerManager.getRegister(instruction.modrm.rm);
      destValue = originalValue & 0xFFFFFFFFn;
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destValue = this.readDwordFromMemory(memoryAddress);
    }

    // 执行XOR操作并保持在32位范围内
    const result = (destValue ^ srcValue) & 0xFFFFFFFFn;

    // 写回结果
    if (isRegister && registerIndex !== undefined && originalValue !== undefined) {
      // 只更新寄存器的低32位
      const newValue = (originalValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeDwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r64, r/m64 指令实现 - 64位变体
  private executeXorRRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值
    const destValue = this.registerManager.getRegister(instruction.modrm.reg);
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(memoryAddress);
    }

    // 执行XOR操作
    const result = destValue ^ srcValue;

    // 写回结果到目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, result);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r8, r/m8 指令实现 - 8位变体
  private executeXorRRm8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值（只取低8位）
    const destRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const destValue = destRegisterValue & 0xFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 执行XOR操作并保持在8位范围内
    const result = (destValue ^ srcValue) & 0xFFn;

    // 写回结果到目标寄存器（只更新低8位）
    const newValue = (destRegisterValue & 0xFFFFFFFFFFFFFF00n) | result;
    this.registerManager.setRegister(instruction.modrm.reg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r16, r/m16 指令实现 - 16位变体
  private executeXorRRm16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值（只取低16位）
    const destRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const destValue = destRegisterValue & 0xFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readWordFromMemory(memoryAddress);
    }

    // 执行XOR操作并保持在16位范围内
    const result = (destValue ^ srcValue) & 0xFFFFn;

    // 写回结果到目标寄存器（只更新低16位）
    const newValue = (destRegisterValue & 0xFFFFFFFFFFFF0000n) | result;
    this.registerManager.setRegister(instruction.modrm.reg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // XOR r32, r/m32 指令实现 - 32位变体
  private executeXorRRm32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标寄存器值（只取低32位）
    const destRegisterValue = this.registerManager.getRegister(instruction.modrm.reg);
    const destValue = destRegisterValue & 0xFFFFFFFFn;
    // 获取源值
    let srcValue: u64;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      srcValue = this.readDwordFromMemory(memoryAddress);
    }

    // 执行XOR操作并保持在32位范围内
    const result = (destValue ^ srcValue) & 0xFFFFFFFFn;

    // 写回结果到目标寄存器（只更新低32位）
    const newValue = (destRegisterValue & 0xFFFFFFFF00000000n) | result;
    this.registerManager.setRegister(instruction.modrm.reg, newValue);

    // 更新标志位
    this.updateFlagsForLogic(destValue, srcValue, result);
  }

  // 旋转指令 - CL寄存器 (RCR CL) 实现
  private executeRcrCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为64位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x3Fn);

    // 执行带进位右旋转
    let result: u64 = value;

    if (count > 0) {
      // 使用临时变量模拟旋转过程
      for (let i = 0; i < count; i++) {
        const oldCf = this.registerManager.getFlag('CF') ? 1n : 0n;
        const newCf = result & 1n;
        result = (result >> 1n) | (oldCf << 63n);
        this.registerManager.setFlag('CF', newCf !== 0n);
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // 压栈操作
  private push(value: u64): void {
    // 减少栈指针
    const stackPointer = this.registerManager.getStackPointer();
    this.registerManager.setStackPointer(stackPointer - 8n);

    // 写入值到栈顶
    this.writeQwordToMemory(stackPointer - 8n, value);
  }

  // 弹栈操作
  private pop(): u64 {
    // 获取栈顶值
    const stackPointer = this.registerManager.getStackPointer();
    const value = this.readQwordFromMemory(stackPointer);

    // 增加栈指针
    this.registerManager.setStackPointer(stackPointer + 8n);

    return value;
  }

  // 更新加法指令的标志位
  private updateFlagsForAdd(src: u64, dst: u64, result: u64): void {
    // CF (Carry Flag) - 如果结果小于任一操作数则设置
    this.registerManager.setFlag('CF', result < src || result < dst);

    // OF (Overflow Flag) - 如果两个正数相加得到负数，或者两个负数相加得到正数则设置
    const srcSign = Number(src) < 0;
    const dstSign = Number(dst) < 0;
    const resultSign = Number(result) < 0;
    this.registerManager.setFlag('OF', (srcSign === dstSign) && (srcSign !== resultSign));

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', resultSign);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(result);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);
  }

  // 更新减法指令的标志位
  private updateFlagsForSub(src: u64, dst: u64, result: u64): void {
    // CF (Carry Flag) - 如果被减数小于减数则设置
    this.registerManager.setFlag('CF', dst < src);

    // OF (Overflow Flag) - 如果正数减负数得到负数，或者负数减正数得到正数则设置
    const srcSign = Number(src) < 0;
    const dstSign = Number(dst) < 0;
    const resultSign = Number(result) < 0;
    this.registerManager.setFlag('OF', (srcSign !== dstSign) && (dstSign !== resultSign));

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', resultSign);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(result);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);
  }

  // 更新带进位加法指令的标志位
  private updateFlagsForAddWithCarry(src: u64, dst: u64, result: u64): void {
    // CF (Carry Flag) - 如果结果小于任一操作数加进位则设置
    const carry = this.registerManager.getFlag('CF') ? 1n : 0n;
    const intermediateResult = dst + carry;
    this.registerManager.setFlag('CF', (intermediateResult < dst) || (result < intermediateResult));

    // OF (Overflow Flag) - 如果两个正数相加（带进位）得到负数，或者两个负数相加（带进位）得到正数则设置
    const srcSign = Number(src) < 0;
    const dstSign = Number(dst) < 0;
    const resultSign = Number(result) < 0;
    this.registerManager.setFlag('OF', (srcSign === dstSign) && (srcSign !== resultSign));

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', resultSign);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(result);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // AF (Auxiliary Carry Flag) - 仅用于8位运算
    if (src < 256n && dst < 256n) {
      const srcLow = src & 0xFn;
      const dstLow = dst & 0xFn;
      const resultLow = result & 0xFn;
      this.registerManager.setFlag('AF', (resultLow < (dstLow + carry)) || ((dstLow + carry) < dstLow));
    }
  }

  // 更新带借位减法指令的标志位
  private updateFlagsForSubWithBorrow(src: u64, dst: u64, result: u64): void {
    // CF (Carry Flag) - 如果被减数减借位小于减数则设置
    const borrow = this.registerManager.getFlag('CF') ? 1n : 0n;
    const adjustedDst = dst - borrow;
    this.registerManager.setFlag('CF', adjustedDst < src);

    // OF (Overflow Flag) - 如果正数减负数（带借位）得到负数，或者负数减正数（带借位）得到正数则设置
    const srcSign = Number(src) < 0;
    const dstSign = Number(dst) < 0;
    const resultSign = Number(result) < 0;
    this.registerManager.setFlag('OF', (srcSign !== dstSign) && (dstSign !== resultSign));

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', resultSign);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(result);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // AF (Auxiliary Carry Flag) - 仅用于8位运算
    if (src < 256n && dst < 256n) {
      const srcLow = src & 0xFn;
      const dstLow = (dst - borrow) & 0xFn;
      this.registerManager.setFlag('AF', dstLow < srcLow);
    }
  }

  // 更新INC/DEC指令的标志位
  private updateFlagsForIncDec(oldValue: u64, newValue: u64): void {
    // CF (Carry Flag) - INC/DEC不影响CF标志位
    // 保持CF标志位不变

    // OF (Overflow Flag) - 对于INC：如果从0x7FFFFFFFFFFFFFFF增加到0x8000000000000000则设置
    // 对于DEC：如果从0x8000000000000000减少到0x7FFFFFFFFFFFFFFF则设置
    const oldSign = Number(oldValue) < 0;
    const newSign = Number(newValue) < 0;
    this.registerManager.setFlag('OF', oldSign !== newSign);

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', newValue === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', newSign);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(newValue);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);
  }

  // 更新移位指令的标志位
  private updateFlagsForShift(result: u64, lastShiftedBit: u64, count: number): void {
    // CF (Carry Flag) - 最后移出的位
    this.registerManager.setFlag('CF', lastShiftedBit !== 0n);

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', Number(result) < 0);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(result);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // OF (Overflow Flag) - 仅当移位计数为1时有效
    if (count === 1) {
      // 对于左移：如果符号位改变则设置OF
      this.registerManager.setFlag('OF', (Number(result) < 0) !== (Number(result >> 1n) < 0));
    }
  }

  // 更新旋转指令的标志位
  private updateFlagsForRotate(result: u64, lastShiftedBit: u64, count: number, oldValue: u64): void {
    // CF (Carry Flag) - 最后移出的位
    this.registerManager.setFlag('CF', lastShiftedBit !== 0n);

    // ZF (Zero Flag) - 如果结果为零则设置
    this.registerManager.setFlag('ZF', result === 0n);

    // SF (Sign Flag) - 结果的最高位
    this.registerManager.setFlag('SF', Number(result) < 0);

    // PF (Parity Flag) - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(result);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // OF (Overflow Flag) - 仅当移位计数为1时有效
    if (count === 1) {
      // 对于旋转指令：如果符号位改变则设置OF
      this.registerManager.setFlag('OF', (Number(result) < 0) !== (Number(oldValue) < 0));
    }
  }

  // ROL - 左旋转 (立即数) - 64位变体
  private executeRolImmediate(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为64位内)
    const count = Number(instruction.immediate) & 0x3F;

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = (value << BigInt(count)) | (value >> BigInt(64 - count));
    }

    // 更新标志位 - 对于ROL指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(64 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 63n) & 1n;
        const newSignBit = (result >> 63n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // ROL - 左旋转 (立即数) - 8位变体
  private executeRolImmediate8(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn; // 只取低8位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 旋转计数 (限制为8位内)
    const count = Number(instruction.immediate) & 0x7;

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value << BigInt(count)) | (value >> BigInt(8 - count))) & 0xFFn; // 保持8位
    }

    // 更新标志位 - 对于ROL指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(8 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 7n) & 1n;
        const newSignBit = (result >> 7n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低8位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeByteToMemory(memoryAddress, Number(result & 0xFFn));
    }
  }

  // ROL - 左旋转 (立即数) - 16位变体
  private executeRolImmediate16(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn; // 只取低16位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readWordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为16位内)
    const count = Number(instruction.immediate) & 0xF;

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value << BigInt(count)) | (value >> BigInt(16 - count))) & 0xFFFFn; // 保持16位
    }

    // 更新标志位 - 对于ROL指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(16 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 15n) & 1n;
        const newSignBit = (result >> 15n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低16位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeWordToMemory(memoryAddress, Number(result & 0xFFFFn));
    }
  }

  // ROL - 左旋转 (立即数) - 32位变体
  private executeRolImmediate32(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn; // 只取低32位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readDwordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为32位内)
    const count = Number(instruction.immediate) & 0x1F;

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value << BigInt(count)) | (value >> BigInt(32 - count))) & 0xFFFFFFFFn; // 保持32位
    }

    // 更新标志位 - 对于ROL指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(32 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 31n) & 1n;
        const newSignBit = (result >> 31n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低32位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeDwordToMemory(memoryAddress, result);
    }
  }

  // INC r/m64 - 递增指令
  private executeIncRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 执行递增操作
    const result = value + 1n;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForIncDec(value, result);
  }

  // DEC r/m64 - 递减指令
  private executeDecRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 执行递减操作
    const result = value - 1n;

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForIncDec(value, result);
  }

  // ROR - 右旋转 (立即数) - 64位变体
  private executeRorImmediate(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为64位内)
    const count = Number(instruction.immediate) & 0x3F;

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = (value >> BigInt(count)) | (value << BigInt(64 - count));
    }

    // 更新标志位 - 对于ROR指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 63n) & 1n;
        const newSignBit = (result >> 63n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // ROR - 右旋转 (立即数) - 8位变体
  private executeRorImmediate8(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn; // 只取低8位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 旋转计数 (限制为8位内)
    const count = Number(instruction.immediate) & 0x7;

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value >> BigInt(count)) | (value << BigInt(8 - count))) & 0xFFn; // 保持8位
    }

    // 更新标志位 - 对于ROR指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 7n) & 1n;
        const newSignBit = (result >> 7n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低8位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeByteToMemory(memoryAddress, Number(result));
    }
  }

  // ROR - 右旋转 (立即数) - 16位变体
  private executeRorImmediate16(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn; // 只取低16位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readWordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为16位内)
    const count = Number(instruction.immediate) & 0xF;

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value >> BigInt(count)) | (value << BigInt(16 - count))) & 0xFFFFn; // 保持16位
    }

    // 更新标志位 - 对于ROR指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 15n) & 1n;
        const newSignBit = (result >> 15n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低16位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeWordToMemory(memoryAddress, Number(result & 0xFFFFn));
    }
  }

  // ROR - 右旋转 (立即数) - 32位变体
  private executeRorImmediate32(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn; // 只取低32位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readDwordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为32位内)
    const count = Number(instruction.immediate) & 0x1F;

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value >> BigInt(count)) | (value << BigInt(32 - count))) & 0xFFFFFFFFn; // 保持32位
    }

    // 更新标志位 - 对于ROR指令，只有CF和OF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 31n) & 1n;
        const newSignBit = (result >> 31n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低32位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeDwordToMemory(memoryAddress, result);
    }
  }

  // RCL - 带进位左旋转 (立即数)
  private executeRclImmediate(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为64位内)
    const count = Number(instruction.immediate) & 0x3F;

    // 获取当前进位标志
    const cf = this.registerManager.getFlag('CF');
    const cfValue = cf ? 1n : 0n;

    // 执行带进位左旋转
    let result: u64 = value;
    let newCf: u64 = 0n;

    if (count > 0) {
      // 对于1位旋转，特殊处理
      if (count === 1) {
        // 保存最高位作为新的CF
        newCf = (result >> 63n) & 1n;
        // 左移1位并将原CF移入最低位
        result = (result << 1n) | cfValue;
      } else {
        // 对于多位旋转，使用临时变量模拟旋转过程
        for (let i = 0; i < count; i++) {
          const oldCf = this.registerManager.getFlag('CF') ? 1n : 0n;
          newCf = (result >> 63n) & 1n;
          result = (result << 1n) | oldCf;
          this.registerManager.setFlag('CF', newCf !== 0n);
        }
      }
    }

    // 更新标志位
    if (count > 0) {
      this.registerManager.setFlag('CF', newCf !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 63n) & 1n;
        const newSignBit = (result >> 63n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // RCR - 带进位右旋转 (立即数)
  private executeRcrImmediate(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 旋转计数 (限制为64位内)
    const count = Number(instruction.immediate) & 0x3F;

    // 获取当前进位标志
    const cf = this.registerManager.getFlag('CF');
    const cfValue = cf ? 1n : 0n;

    // 执行带进位右旋转
    let result: u64 = value;
    let newCf: u64 = 0n;

    if (count > 0) {
      // 对于1位旋转，特殊处理
      if (count === 1) {
        // 保存最低位作为新的CF
        newCf = result & 1n;
        // 右移1位并将原CF移入最高位
        result = (result >> 1n) | (cfValue << 63n);
      } else {
        // 对于多位旋转，使用临时变量模拟旋转过程
        for (let i = 0; i < count; i++) {
          const oldCf = this.registerManager.getFlag('CF') ? 1n : 0n;
          newCf = result & 1n;
          result = (result >> 1n) | (oldCf << 63n);
          this.registerManager.setFlag('CF', newCf !== 0n);
        }
      }
    }

    // 更新标志位
    if (count > 0) {
      this.registerManager.setFlag('CF', newCf !== 0n);

      // 溢出标志 (OF) - 对于1位旋转，设置为符号位是否改变
      if (count === 1) {
        const oldSignBit = (value >> 63n) & 1n;
        const newSignBit = (result >> 63n) & 1n;
        this.registerManager.setFlag('OF', oldSignBit !== newSignBit);
      } else if (count > 1) {
        // 旋转计数大于1时，溢出标志未定义（这里保持不变）
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // 旋转指令 - CL寄存器 (ROL CL) - 64位变体
  private executeRolCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为64位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x3Fn);

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = (value << BigInt(count)) | (value >> BigInt(64 - count));
    }

    // 更新标志位 - 对于ROL CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(64 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // 旋转指令 - CL寄存器 (ROL CL) - 8位变体
  private executeRolCl8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn; // 只取低8位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 从CL寄存器获取旋转计数 (限制为8位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x7n);

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value << BigInt(count)) | (value >> BigInt(8 - count))) & 0xFFn; // 保持8位
    }

    // 更新标志位 - 对于ROL CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(8 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低8位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeByteToMemory(memoryAddress, Number(result & 0xFFn));
    }
  }

  // 旋转指令 - CL寄存器 (ROL CL) - 16位变体
  private executeRolCl16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn; // 只取低16位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readWordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为16位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0xFn);

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value << BigInt(count)) | (value >> BigInt(16 - count))) & 0xFFFFn; // 保持16位
    }

    // 更新标志位 - 对于ROL CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(16 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低16位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeWordToMemory(memoryAddress, Number(result & 0xFFFFn));
    }
  }

  // 旋转指令 - CL寄存器 (ROL CL) - 32位变体
  private executeRolCl32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn; // 只取低32位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readDwordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为32位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x1Fn);

    // 执行左旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value << BigInt(count)) | (value >> BigInt(32 - count))) & 0xFFFFFFFFn; // 保持32位
    }

    // 更新标志位 - 对于ROL CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(32 - count)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低32位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeDwordToMemory(memoryAddress, result);
    }
  }

  // 旋转指令 - CL寄存器 (ROR CL) - 64位变体
  private executeRorCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为64位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x3Fn);

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = (value >> BigInt(count)) | (value << BigInt(64 - count));
    }

    // 更新标志位 - 对于ROR CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // 旋转指令 - CL寄存器 (ROR CL) - 8位变体
  private executeRorCl8(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn; // 只取低8位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = BigInt(this.readByteFromMemory(memoryAddress));
    }

    // 从CL寄存器获取旋转计数 (限制为8位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x7n);

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value >> BigInt(count)) | (value << BigInt(8 - count))) & 0xFFn; // 保持8位
    }

    // 更新标志位 - 对于ROR CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低8位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeByteToMemory(memoryAddress, Number(result));
    }
  }

  // 旋转指令 - CL寄存器 (ROR CL) - 16位变体
  private executeRorCl16(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn; // 只取低16位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readWordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为16位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0xFn);

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value >> BigInt(count)) | (value << BigInt(16 - count))) & 0xFFFFn; // 保持16位
    }

    // 更新标志位 - 对于ROR CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低16位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFFFFFF0000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeWordToMemory(memoryAddress, Number(result & 0xFFFFn));
    }
  }

  // 旋转指令 - CL寄存器 (ROR CL) - 32位变体
  private executeRorCl32(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn; // 只取低32位
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readDwordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为32位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x1Fn);

    // 执行右旋转
    let result: u64;
    if (count === 0) {
      result = value; // 旋转计数为0时，结果不变
    } else {
      result = ((value >> BigInt(count)) | (value << BigInt(32 - count))) & 0xFFFFFFFFn; // 保持32位
    }

    // 更新标志位 - 对于ROR CL指令，只有CF会被更新
    if (count > 0) {
      // 进位标志 (CF) - 设置为最后移出的位
      const lastBit = (value >> BigInt(count - 1)) & 1n;
      this.registerManager.setFlag('CF', lastBit !== 0n);
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      // 只更新寄存器的低32位
      const currentValue = this.registerManager.getRegister(registerIndex);
      const newValue = (currentValue & 0xFFFFFFFF00000000n) | result;
      this.registerManager.setRegister(registerIndex, newValue);
    } else if (memoryAddress !== undefined) {
      this.writeDwordToMemory(memoryAddress, result);
    }
  }

  // 旋转指令 - CL寄存器 (RCL CL) 实现
  private executeRclCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 从CL寄存器获取旋转计数 (限制为64位内)
    const clValue = this.registerManager.getRegister(Registers.RCX);
    const count = Number(clValue & 0x3Fn);

    // 执行带进位左旋转
    let result: u64 = value;

    if (count > 0) {
      // 使用临时变量模拟旋转过程
      for (let i = 0; i < count; i++) {
        const oldCf = this.registerManager.getFlag('CF') ? 1n : 0n;
        const newCf = (result >> 63n) & 1n;
        result = (result << 1n) | oldCf;
        this.registerManager.setFlag('CF', newCf !== 0n);
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }
  }

  // SHL r/m64, CL指令 实现
  private executeShlCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取CL寄存器中的移位计数 (CL是RCX的低8位)
    const rcxValue = this.registerManager.getRegister(Registers.RCX);
    const clValue = Number(rcxValue & 0xFFn) & 0x3F;
    if (clValue === 0) return;

    const addr = this.calculateEffectiveAddress(instruction);
    let value = this.readQwordFromMemory(addr);

    // 保存移位前的最高位用于CF计算
    const highestBit = (value >> 63n) & 1n;
    // 执行左移操作
    value <<= BigInt(clValue);
    this.writeQwordToMemory(addr, value);

    // 更新标志位
    this.updateFlagsForShift(value, highestBit, clValue);
  }

  // SHR r/m64, CL指令 实现
  private executeShrCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取CL寄存器中的移位计数 (CL是RCX的低8位)
    const rcxValue = this.registerManager.getRegister(Registers.RCX);
    const clValue = Number(rcxValue & 0xFFn) & 0x3F;
    if (clValue === 0) return;

    const addr = this.calculateEffectiveAddress(instruction);
    let value = this.readQwordFromMemory(addr);

    // 保存移位前的最低位用于CF计算
    const lowestBit = value & 1n;
    // 执行无符号右移操作
    value = value >> BigInt(clValue);
    this.writeQwordToMemory(addr, value);

    // 更新标志位
    this.updateFlagsForShift(value, lowestBit, clValue);
  }

  // SAR r/m64, imm8 - 算术右移指令（立即数）
  private executeSarImm(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 移位计数 (限制为64位内)
    const count = Number(instruction.immediate) & 0x3F;
    if (count === 0) return;

    // 保存移位前的最低位用于CF计算
    const lowestBit = value & 1n;

    // 执行算术右移 - 对于算术右移，我们需要保持符号位不变
    // 检查符号位
    const signBit = value >> 63n;

    // 对于算术右移，我们需要在右移时保持符号位
    let result: u64;
    if (count === 64) {
      // 移动64位时，所有位都变为符号位
      result = signBit ? 0xFFFFFFFFFFFFFFFFn : 0n;
    } else {
      // 执行右移并保留符号位
      result = value >> BigInt(count);
      // 如果是负数，设置最高的count位为1
      if (signBit !== 0n) {
        result |= (0xFFFFFFFFFFFFFFFFn << BigInt(64 - count));
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForShift(result, lowestBit, count);
  }

  // SAR r/m64, CL - 算术右移指令（CL寄存器）
  private executeSarCl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    let value: u64;
    let isRegister = false;
    let registerIndex: Registers | undefined;
    let memoryAddress: u64 | undefined;

    // 如果rm是寄存器
    if (instruction.modrm.mod === 3) {
      value = this.registerManager.getRegister(instruction.modrm.rm);
      isRegister = true;
      registerIndex = instruction.modrm.rm;
    } else {
      memoryAddress = this.calculateEffectiveAddress(instruction);
      value = this.readQwordFromMemory(memoryAddress);
    }

    // 获取CL寄存器中的移位计数 (CL是RCX的低8位)
    const rcxValue = this.registerManager.getRegister(Registers.RCX);
    const clValue = Number(rcxValue & 0xFFn) & 0x3F;
    if (clValue === 0) return;

    // 保存移位前的最低位用于CF计算
    const lowestBit = value & 1n;

    // 执行算术右移 - 对于算术右移，我们需要保持符号位不变
    // 检查符号位
    const signBit = value >> 63n;

    // 对于算术右移，我们需要在右移时保持符号位
    let result: u64;
    if (clValue === 64) {
      // 移动64位时，所有位都变为符号位
      result = signBit ? 0xFFFFFFFFFFFFFFFFn : 0n;
    } else {
      // 执行右移并保留符号位
      result = value >> BigInt(clValue);
      // 如果是负数，设置最高的clValue位为1
      if (signBit !== 0n) {
        result |= (0xFFFFFFFFFFFFFFFFn << BigInt(64 - clValue));
      }
    }

    // 写回结果
    if (isRegister && registerIndex !== undefined) {
      this.registerManager.setRegister(registerIndex, result);
    } else if (memoryAddress !== undefined) {
      this.writeQwordToMemory(memoryAddress, result);
    }

    // 更新标志位
    this.updateFlagsForShift(result, lowestBit, clValue);
  }

  // MOVSB - 字节串移动指令
  private executeMovsb(instruction: DecodedInstruction): void {
    // 获取源地址 (RSI)
    const sourceAddress = this.registerManager.getRegister(Registers.RSI);
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);

    // 读取源字节
    const sourceByte = this.readByteFromMemory(sourceAddress);
    // 写入目标地址
    this.writeByteToMemory(destinationAddress, sourceByte);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RSI, sourceAddress - 1n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 1n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RSI, sourceAddress + 1n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 1n);
    }
  }

  // MOVS - 字串/双字串/四字串移动指令
  private executeMovs(instruction: DecodedInstruction): void {
    // 默认使用四字串移动
    // 获取源地址 (RSI)
    const sourceAddress = this.registerManager.getRegister(Registers.RSI);
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);

    // 读取源四字
    const sourceValue = this.readQwordFromMemory(sourceAddress);
    // 写入目标地址
    this.writeQwordToMemory(destinationAddress, sourceValue);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RSI, sourceAddress - 8n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 8n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RSI, sourceAddress + 8n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 8n);
    }
  }

  // STOSB - 存储字节串指令
  private executeStosb(instruction: DecodedInstruction): void {
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);
    // 获取AL寄存器的值（低8位）
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;

    // 写入目标地址
    this.writeByteToMemory(destinationAddress, Number(alValue));

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 1n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 1n);
    }
  }

  // STOS - 存储字串/双字串/四字串指令
  private executeStos(instruction: DecodedInstruction): void {
    // 默认使用四字串存储
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);
    // 获取RAX寄存器的值
    const raxValue = this.registerManager.getRegister(Registers.RAX);

    // 写入目标地址
    this.writeQwordToMemory(destinationAddress, raxValue);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 8n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 8n);
    }
  }

  // LODSB - 加载字节串指令
  private executeLodsb(instruction: DecodedInstruction): void {
    // 获取源地址 (RSI)
    const sourceAddress = this.registerManager.getRegister(Registers.RSI);

    // 读取源字节
    const sourceByte = this.readByteFromMemory(sourceAddress);
    // 保存到AL寄存器（低8位）
    const currentRax = this.registerManager.getRegister(Registers.RAX);
    const newRax = (currentRax & 0xFFFFFFFFFFFFFF00n) | BigInt(sourceByte);
    this.registerManager.setRegister(Registers.RAX, newRax);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RSI, sourceAddress - 1n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RSI, sourceAddress + 1n);
    }
  }

  // LODS - 加载字串/双字串/四字串指令
  private executeLods(instruction: DecodedInstruction): void {
    // 默认使用四字串加载
    // 获取源地址 (RSI)
    const sourceAddress = this.registerManager.getRegister(Registers.RSI);

    // 读取源四字
    const sourceValue = this.readQwordFromMemory(sourceAddress);
    // 保存到RAX寄存器
    this.registerManager.setRegister(Registers.RAX, sourceValue);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RSI, sourceAddress - 8n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RSI, sourceAddress + 8n);
    }
  }

  // CMPSB - 字节串比较指令
  private executeCmpsb(instruction: DecodedInstruction): void {
    // 获取源地址 (RSI)
    const sourceAddress = this.registerManager.getRegister(Registers.RSI);
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);

    // 读取源字节和目标字节
    const sourceByte = this.readByteFromMemory(sourceAddress);
    const destByte = this.readByteFromMemory(destinationAddress);

    // 执行比较操作并更新标志位
    const sourceValue = BigInt(sourceByte);
    const destValue = BigInt(destByte);
    const result = sourceValue - destValue;

    // 更新标志位：ZF, SF, PF, CF, OF
    this.registerManager.setFlag('ZF', sourceValue === destValue);
    this.registerManager.setFlag('SF', (result & 0x80n) !== 0n);
    // 计算奇偶标志 (PF) - 计算结果中1的个数是否为偶数
    const parityValue = Number(result & 0xFFn);
    let count = 0;
    for (let i = 0; i < 8; i++) {
      count += (parityValue >> i) & 1;
    }
    this.registerManager.setFlag('PF', (count % 2) === 0);
    // 计算进位标志 (CF) - 无符号减法是否产生借位
    this.registerManager.setFlag('CF', sourceValue < destValue);
    // 计算溢出标志 (OF) - 有符号减法是否产生溢出
    const sourceSign = (sourceValue & 0x80n) !== 0n;
    const destSign = (destValue & 0x80n) !== 0n;
    const resultSign = (result & 0x80n) !== 0n;
    this.registerManager.setFlag('OF', sourceSign !== destSign && resultSign === destSign);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RSI, sourceAddress - 1n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 1n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RSI, sourceAddress + 1n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 1n);
    }
  }

  // CMPS - 字串/双字串/四字串比较指令
  private executeCmps(instruction: DecodedInstruction): void {
    // 默认使用四字串比较
    // 获取源地址 (RSI)
    const sourceAddress = this.registerManager.getRegister(Registers.RSI);
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);

    // 读取源四字和目标四字
    const sourceValue = this.readQwordFromMemory(sourceAddress);
    const destValue = this.readQwordFromMemory(destinationAddress);

    // 执行比较操作并更新标志位
    const result = sourceValue - destValue;

    // 更新标志位：ZF, SF, PF, CF, OF
    this.registerManager.setFlag('ZF', sourceValue === destValue);
    this.registerManager.setFlag('SF', (result & 0x8000000000000000n) !== 0n);
    // 计算奇偶标志 (PF) - 只关注结果的最低8位
    const parityValue = Number(result & 0xFFn);
    let count = 0;
    for (let i = 0; i < 8; i++) {
      count += (parityValue >> i) & 1;
    }
    this.registerManager.setFlag('PF', (count % 2) === 0);
    // 计算进位标志 (CF) - 无符号减法是否产生借位
    this.registerManager.setFlag('CF', sourceValue < destValue);
    // 计算溢出标志 (OF) - 有符号减法是否产生溢出
    const sourceSign = (sourceValue & 0x8000000000000000n) !== 0n;
    const destSign = (destValue & 0x8000000000000000n) !== 0n;
    const resultSign = (result & 0x8000000000000000n) !== 0n;
    this.registerManager.setFlag('OF', sourceSign !== destSign && resultSign === destSign);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RSI, sourceAddress - 8n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 8n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RSI, sourceAddress + 8n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 8n);
    }
  }

  // SCASB - 字节串扫描指令
  private executeScasb(instruction: DecodedInstruction): void {
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);
    // 获取AL寄存器的值（低8位）
    const alValue = this.registerManager.getRegister(Registers.RAX) & 0xFFn;

    // 读取目标字节
    const destByte = this.readByteFromMemory(destinationAddress);
    const destValue = BigInt(destByte);

    // 执行比较操作并更新标志位
    const result = alValue - destValue;

    // 更新标志位：ZF, SF, PF, CF, OF
    this.registerManager.setFlag('ZF', alValue === destValue);
    this.registerManager.setFlag('SF', (result & 0x80n) !== 0n);
    // 计算奇偶标志 (PF) - 计算结果中1的个数是否为偶数
    const parityValue = Number(result & 0xFFn);
    let count = 0;
    for (let i = 0; i < 8; i++) {
      count += (parityValue >> i) & 1;
    }
    this.registerManager.setFlag('PF', (count % 2) === 0);
    // 计算进位标志 (CF) - 无符号减法是否产生借位
    this.registerManager.setFlag('CF', alValue < destValue);
    // 计算溢出标志 (OF) - 有符号减法是否产生溢出
    const alSign = (alValue & 0x80n) !== 0n;
    const destSign = (destValue & 0x80n) !== 0n;
    const resultSign = (result & 0x80n) !== 0n;
    this.registerManager.setFlag('OF', alSign !== destSign && resultSign === destSign);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 1n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 1n);
    }
  }

  // SCAS - 字串/双字串/四字串扫描指令
  private executeScas(instruction: DecodedInstruction): void {
    // 默认使用四字串扫描
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);
    // 获取RAX寄存器的值
    const raxValue = this.registerManager.getRegister(Registers.RAX);

    // 读取目标四字
    const destValue = this.readQwordFromMemory(destinationAddress);

    // 执行比较操作并更新标志位
    const result = raxValue - destValue;

    // 更新标志位：ZF, SF, PF, CF, OF
    this.registerManager.setFlag('ZF', raxValue === destValue);
    this.registerManager.setFlag('SF', (result & 0x8000000000000000n) !== 0n);
    // 计算奇偶标志 (PF) - 只关注结果的最低8位
    const parityValue = Number(result & 0xFFn);
    let count = 0;
    for (let i = 0; i < 8; i++) {
      count += (parityValue >> i) & 1;
    }
    this.registerManager.setFlag('PF', (count % 2) === 0);
    // 计算进位标志 (CF) - 无符号减法是否产生借位
    this.registerManager.setFlag('CF', raxValue < destValue);
    // 计算溢出标志 (OF) - 有符号减法是否产生溢出
    const raxSign = (raxValue & 0x8000000000000000n) !== 0n;
    const destSign = (destValue & 0x8000000000000000n) !== 0n;
    const resultSign = (result & 0x8000000000000000n) !== 0n;
    this.registerManager.setFlag('OF', raxSign !== destSign && resultSign === destSign);

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 8n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 8n);
    }
  }

  // =============================================================================
  // 条件设置指令 (SETcc) 实现
  // =============================================================================

  /**
   * SETcc 指令的通用实现
   * @param instruction 解码后的指令
   * @param condition 条件函数，返回true或false决定是否设置字节为1
   */
  private executeSetcc(instruction: DecodedInstruction, condition: () => boolean): void {
    if (!instruction.modrm) return;

    // 计算目标操作数的地址
    const addr = this.calculateEffectiveAddress(instruction);

    // 根据条件设置字节值（1或0）
    const value = condition() ? 1 : 0;
    this.writeByteToMemory(addr, value);
  }

  // SETO - 如果溢出则设置字节
  private executeSeto(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('OF'));
  }

  // SETNO - 如果未溢出则设置字节
  private executeSetno(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => !this.registerManager.getFlag('OF'));
  }

  // SETB/SETNAE - 如果低于/不高于且不等于则设置字节
  private executeSetb(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('CF'));
  }

  // SETAE/SETNB - 如果高于或等于/不低于则设置字节
  private executeSetae(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => !this.registerManager.getFlag('CF'));
  }

  // SETE/SETZ - 如果等于/为零则设置字节
  private executeSete(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('ZF'));
  }

  // SETNE/SETNZ - 如果不等于/不为零则设置字节
  private executeSetne(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => !this.registerManager.getFlag('ZF'));
  }

  // SETBE/SETNA - 如果低于或等于/不高于则设置字节
  private executeSetbe(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('CF') || this.registerManager.getFlag('ZF'));
  }

  // SETA/SETNBE - 如果高于/不低于且不等于则设置字节
  private executeSeta(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => !this.registerManager.getFlag('CF') && !this.registerManager.getFlag('ZF'));
  }

  // SETS - 如果符号位为1则设置字节
  private executeSets(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('SF'));
  }

  // SETNS - 如果符号位为0则设置字节
  private executeSetns(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => !this.registerManager.getFlag('SF'));
  }

  // SETP/SETPE - 如果奇偶位为偶则设置字节
  private executeSetp(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('PF'));
  }

  // SETNP/SETPO - 如果奇偶位为奇则设置字节
  private executeSetnp(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => !this.registerManager.getFlag('PF'));
  }

  // SETL/SETNGE - 如果小于/不大于且不等于则设置字节
  private executeSetl(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('SF') !== this.registerManager.getFlag('OF'));
  }

  // SETGE/SETNL - 如果大于或等于/不小于则设置字节
  private executeSetge(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => this.registerManager.getFlag('SF') === this.registerManager.getFlag('OF'));
  }

  // SETLE/SETNG - 如果小于或等于/不大于则设置字节
  private executeSetle(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => (this.registerManager.getFlag('SF') !== this.registerManager.getFlag('OF')) || this.registerManager.getFlag('ZF'));
  }

  // SETG/SETNLE - 如果大于/不小于且不等于则设置字节
  private executeSetg(instruction: DecodedInstruction): void {
    this.executeSetcc(instruction, () => (this.registerManager.getFlag('SF') === this.registerManager.getFlag('OF')) && !this.registerManager.getFlag('ZF'));
  }

  // =============================================================================
  // 条件移动指令 (CMOVcc) 实现
  // =============================================================================

  /**
   * CMOVcc 指令的通用实现
   * @param instruction 解码后的指令
   * @param condition 条件函数，返回true或false决定是否执行移动
   */
  private executeCmovcc(instruction: DecodedInstruction, condition: () => boolean): void {
    if (!instruction.modrm) return;

    // 如果条件不满足，不执行任何操作
    if (!condition()) return;

    // 获取源操作数的值
    let srcValue: u64;
    if (instruction.modrm.mod === 3) {
      // 源操作数是寄存器
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      // 源操作数在内存中
      const srcAddr = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(srcAddr);
    }

    // 目标操作数始终是寄存器
    const dstReg = instruction.modrm.reg;
    this.registerManager.setRegister(dstReg, srcValue);
  }

  // CMOVA/CMOVNBE - 如果高于/不低于且不等于则移动
  private executeCmova(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => !this.registerManager.getFlag('CF') && !this.registerManager.getFlag('ZF'));
  }

  // CMOVAE/CMOVNB - 如果高于或等于/不低于则移动
  private executeCmovae(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => !this.registerManager.getFlag('CF'));
  }

  // CMOVB/CMOVNAE - 如果低于/不高于且不等于则移动
  private executeCmovb(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('CF'));
  }

  // CMOVBE/CMOVNA - 如果低于或等于/不高于则移动
  private executeCmovbe(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('CF') || this.registerManager.getFlag('ZF'));
  }

  // CMOVE/CMOVZ - 如果等于/为零则移动
  private executeCmove(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('ZF'));
  }

  // CMOVG/CMOVNLE - 如果大于/不小于且不等于则移动
  private executeCmovg(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => (this.registerManager.getFlag('SF') === this.registerManager.getFlag('OF')) && !this.registerManager.getFlag('ZF'));
  }

  // CMOVGE/CMOVNL - 如果大于或等于/不小于则移动
  private executeCmovge(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('SF') === this.registerManager.getFlag('OF'));
  }

  // CMOVL/CMOVNGE - 如果小于/不大于且不等于则移动
  private executeCmovl(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('SF') !== this.registerManager.getFlag('OF'));
  }

  // CMOVLE/CMOVNG - 如果小于或等于/不大于则移动
  private executeCmovle(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => (this.registerManager.getFlag('SF') !== this.registerManager.getFlag('OF')) || this.registerManager.getFlag('ZF'));
  }

  // CMOVNE/CMOVNZ - 如果不等于/不为零则移动
  private executeCmovne(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => !this.registerManager.getFlag('ZF'));
  }

  // CMOVNO - 如果未溢出则移动
  private executeCmovno(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => !this.registerManager.getFlag('OF'));
  }

  // CMOVNP/CMOVPO - 如果奇偶位为奇则移动
  private executeCmovnp(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => !this.registerManager.getFlag('PF'));
  }

  // CMOVNS - 如果符号位为0则移动
  private executeCmovns(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => !this.registerManager.getFlag('SF'));
  }

  // CMOVO - 如果溢出则移动
  private executeCmovo(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('OF'));
  }

  // CMOVP/CMOVPE - 如果奇偶位为偶则移动
  private executeCmovp(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('PF'));
  }

  // CMOVS - 如果符号位为1则移动
  private executeCmovs(instruction: DecodedInstruction): void {
    this.executeCmovcc(instruction, () => this.registerManager.getFlag('SF'));
  }

  // =============================================================================
  // 循环指令实现
  // =============================================================================

  // SALC - 设置AL为进位标志
  private executeSalc(): void {
    // 检查进位标志
    const cf = this.registerManager.getFlag('CF');
    
    // 根据进位标志设置AL寄存器
    const alValue = cf ? 255n : 0n;
    
    // 更新AX寄存器（只修改AL部分）
    const currentValue = this.registerManager.getRegister(Registers.RAX);
    const newValue = (currentValue & 0xFFFFFFFFFFFFFF00n) | alValue;
    this.registerManager.setRegister(Registers.RAX, newValue);
  }

  // INT 1 - 中断1指令
  private executeInterrupt1(): void {
    // 保存FLAGS寄存器
    // 获取标志寄存器值
    const flags = this.registerManager.getFlagsRegister();
    
    // 清除IF和TF标志
    this.registerManager.setFlag('IF', false);
    this.registerManager.setFlag('TF', false);
    
    // 获取FLAGS、CS和IP压入栈中
    const cs = this.registerManager.getSegmentRegister(SegmentRegisters.CS);
    const ip = this.registerManager.getInstructionPointer();
    
    this.push(flags);
    this.push(BigInt(cs));
    this.push(ip);
    
    // 从中断向量表获取中断1的处理程序地址
    // 在实际实现中，这里应该从IDT中获取
    // 这里使用模拟值，假设中断1的处理程序地址是0x0000:0x0004
    const handlerOffset = 0x0004n;
    const handlerSegment = 0x0000n;

    // 设置CS和IP为中断处理程序的地址
    this.registerManager.setSegmentRegister(SegmentRegisters.CS, Number(handlerSegment));
    this.registerManager.setInstructionPointer(handlerOffset);
  }

  // 双移位指令（SHLD/SHRD）
  private executeDoubleShift(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;
    
    // 获取移位次数
    let count: u64;
    if (instruction.immediate !== undefined) {
      // 立即数指定移位次数
      count = BigInt(instruction.immediate);
    } else {
      // 从CL寄存器获取移位次数
      count = this.registerManager.getRegister(Registers.RCX) & 0xFFn;
    }
    
    // 移位次数限制为63
    count = count % 64n;
    
    // 获取目标操作数的值
    let destination: u64;
    let memoryAddress: u64 | undefined;
    
    if (instruction.modrm.mod === 3) {
      // 目标操作数是寄存器
      destination = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      // 目标操作数在内存中
      memoryAddress = this.calculateEffectiveAddress(instruction);
      destination = this.readQwordFromMemory(memoryAddress);
    }
    
    // 获取源操作数（寄存器）的值
    const source = this.registerManager.getRegister(instruction.modrm.reg);
    
    // 根据操作码确定是左移还是右移
    let result: u64;
    let carryFlag: boolean = false;
    
    if (instruction.opcode === 0xA4 || instruction.opcode === 0xA5) {
      // SHLD - 双精度左移
      if (count > 0n) {
        // 计算移位后的值
        result = (destination << count) | (source >> (64n - count));
        // 设置进位标志为移位出去的最高位
        carryFlag = (Number(destination >> (64n - count)) & 1) === 1;
      } else {
        result = destination;
      }
    } else {
      // SHRD - 双精度右移
      if (count > 0n) {
        // 计算移位后的值
        result = (destination >> count) | (source << (64n - count));
        // 设置进位标志为移位出去的最低位
        carryFlag = Number(destination << (64n - count)) !== 0;
      } else {
        result = destination;
      }
    }
    
    // 更新进位标志
    this.registerManager.setFlag('CF', carryFlag);
    
    // 保存结果
    if (instruction.modrm.mod === 3) {
      // 结果保存到寄存器
      this.registerManager.setRegister(instruction.modrm.rm, result);
    } else {
      // 结果保存到内存
      this.writeQwordToMemory(memoryAddress!, result);
    }

    // 更新其他标志位
    // 注意：updateFlagsForShift方法需要适应bigint类型参数
  }

  // PUSH seg - 压入段寄存器
  private executePushSeg(segmentReg: SegmentRegisters): void {
    // 获取段寄存器的值
    const segValue = BigInt(this.registerManager.getSegmentRegister(segmentReg));
    
    // 压入栈中
    this.push(segValue);
  }

  // POP seg - 弹出段寄存器
  private executePopSeg(segmentReg: SegmentRegisters): void {
    // 从栈中弹出值
    const value = this.pop();
    
    // CS不能通过POP指令直接修改
    if (segmentReg === SegmentRegisters.CS) {
      return;
    }
    
    // 设置段寄存器的值
    this.registerManager.setSegmentRegister(segmentReg, Number(value & 0xFFFFn));
  }

  // LOOP - 循环指令，递减ECX并跳转
  private executeLoop(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    // 获取ECX寄存器的值（低32位）
    let ecx = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;

    // 递减ECX
    ecx -= 1n;

    // 保存更新后的ECX值
    this.registerManager.setRegister(Registers.RCX, (this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFF00000000n) | ecx);

    // 如果ECX不为零，则跳转到目标地址
    if (ecx !== 0n) {
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // LOOPE/LOOPZ - 如果ECX不为零且ZF为1则跳转
  private executeLoope(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    // 获取ECX寄存器的值（低32位）
    let ecx = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;

    // 递减ECX
    ecx -= 1n;

    // 保存更新后的ECX值
    this.registerManager.setRegister(Registers.RCX, (this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFF00000000n) | ecx);

    // 如果ECX不为零且ZF为1，则跳转到目标地址
    if (ecx !== 0n && this.registerManager.getFlag('ZF')) {
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // LOOPNE/LOOPNZ - 如果ECX不为零且ZF为0则跳转
  private executeLoopne(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    // 获取ECX寄存器的值（低32位）
    let ecx = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;

    // 递减ECX
    ecx -= 1n;

    // 保存更新后的ECX值
    this.registerManager.setRegister(Registers.RCX, (this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFF00000000n) | ecx);

    // 如果ECX不为零且ZF为0，则跳转到目标地址
    if (ecx !== 0n && !this.registerManager.getFlag('ZF')) {
      const rip = this.registerManager.getInstructionPointer();
      this.registerManager.setInstructionPointer(rip + instruction.immediate);
    }
  }

  // =============================================================================
  // 扩展位操作指令实现
  // =============================================================================

  // LZCNT - 前导零计数指令
  private executeLzcnt(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数的值
    let srcValue: u64;
    if (instruction.modrm.mod === 3) {
      // 源操作数是寄存器
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      // 源操作数在内存中
      const srcAddr = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(srcAddr);
    }

    // 计算前导零的个数
    let count = 0;
    if (srcValue === 0n) {
      // 如果值为0，前导零的个数为64
      count = 64;
    } else {
      // 否则，计算前导零的个数
      const value = Number(srcValue);
      count = 63 - Math.clz32(value >>> 0);
    }

    // 目标操作数始终是寄存器
    const dstReg = instruction.modrm.reg;
    this.registerManager.setRegister(dstReg, BigInt(count));
  }

  // TZCNT - 尾随零计数指令
  private executeTzcnt(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数的值
    let srcValue: u64;
    if (instruction.modrm.mod === 3) {
      // 源操作数是寄存器
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      // 源操作数在内存中
      const srcAddr = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(srcAddr);
    }

    // 计算尾随零的个数
    let count = 0;
    if (srcValue === 0n) {
      // 如果值为0，尾随零的个数为64
      count = 64;
    } else {
      // 否则，计算尾随零的个数
      const value = Number(srcValue);
      count = Math.clz32(value & -value) ^ 31;
    }

    // 目标操作数始终是寄存器
    const dstReg = instruction.modrm.reg;
    this.registerManager.setRegister(dstReg, BigInt(count));
  }

  // POPCNT - 位数计数指令
  private executePopcnt(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取源操作数的值
    let srcValue: u64;
    if (instruction.modrm.mod === 3) {
      // 源操作数是寄存器
      srcValue = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      // 源操作数在内存中
      const srcAddr = this.calculateEffectiveAddress(instruction);
      srcValue = this.readQwordFromMemory(srcAddr);
    }

    // 计算1的个数
    let count = 0;
    let value = srcValue;
    while (value !== 0n) {
      value &= value - 1n;
      count++;
    }

    // 目标操作数始终是寄存器
    const dstReg = instruction.modrm.reg;
    this.registerManager.setRegister(dstReg, BigInt(count));
  }

  // =============================================================================
  // BCD运算指令实现
  // =============================================================================

  // DAA - 加法后的十进制调整
  private executeDaa(): void {
    // 获取AL寄存器的值（低8位）
    let al = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const cf = this.registerManager.getFlag('CF');
    const af = this.registerManager.getFlag('AF'); // 假设AF标志可以通过某种方式获取

    // 低4位调整
    if ((al & 0x0Fn) > 9n || af) {
      al += 6n;
    }

    // 高4位调整
    if (al > 0x9F || cf) {
      al += 0x60n;
      this.registerManager.setFlag('CF', true);
    } else {
      this.registerManager.setFlag('CF', false);
    }

    // 更新AL寄存器
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFFFF00n) | al);

    // 更新其他标志位
    this.updateFlagsForDaa(al);
  }

  // 更新DAA指令的标志位
  private updateFlagsForDaa(value: u64): void {
    // ZF - 如果结果为零则设置
    this.registerManager.setFlag('ZF', value === 0n);

    // SF - 结果的最高位
    this.registerManager.setFlag('SF', Number(value) < 0);

    // PF - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(value);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // OF标志未定义
  }

  // DAS - 减法后的十进制调整
  private executeDas(): void {
    // 获取AL寄存器的值（低8位）
    let al = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    const cf = this.registerManager.getFlag('CF');
    const af = this.registerManager.getFlag('AF'); // 假设AF标志可以通过某种方式获取

    // 低4位调整
    if ((al & 0x0Fn) > 9n || af) {
      al -= 6n;
    }

    // 高4位调整
    if (al > 0x9F || cf) {
      al -= 0x60n;
      this.registerManager.setFlag('CF', true);
    } else {
      this.registerManager.setFlag('CF', false);
    }

    // 更新AL寄存器
    this.registerManager.setRegister(Registers.RAX, (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFFFF00n) | al);

    // 更新其他标志位
    this.updateFlagsForDaa(al); // 复用DAA的标志位更新逻辑
  }

  // AAA - 加法后的ASCII调整
  private executeAaa(): void {
    // 获取AL寄存器的值（低8位）
    let al = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    let ah = (this.registerManager.getRegister(Registers.RAX) >> 8n) & 0xFFn;
    const af = this.registerManager.getFlag('AF'); // 假设AF标志可以通过某种方式获取

    // 如果低4位大于9或AF为1
    if ((al & 0x0Fn) > 9n || af) {
      al += 6n;
      ah += 1n;
      this.registerManager.setFlag('CF', true);
      this.registerManager.setFlag('AF', true);
    } else {
      this.registerManager.setFlag('CF', false);
      this.registerManager.setFlag('AF', false);
    }

    // 清除AL的高4位
    al &= 0x0Fn;

    // 更新AX寄存器
    this.registerManager.setRegister(Registers.RAX, (ah << 8n) | al);
  }

  // AAS - 减法后的ASCII调整
  private executeAas(): void {
    // 获取AL寄存器的值（低8位）
    let al = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    let ah = (this.registerManager.getRegister(Registers.RAX) >> 8n) & 0xFFn;
    const af = this.registerManager.getFlag('AF'); // 假设AF标志可以通过某种方式获取

    // 如果低4位大于9或AF为1
    if ((al & 0x0Fn) > 9n || af) {
      al -= 6n;
      ah -= 1n;
      this.registerManager.setFlag('CF', true);
      this.registerManager.setFlag('AF', true);
    } else {
      this.registerManager.setFlag('CF', false);
      this.registerManager.setFlag('AF', false);
    }

    // 清除AL的高4位
    al &= 0x0Fn;

    // 更新AX寄存器
    this.registerManager.setRegister(Registers.RAX, (ah << 8n) | al);
  }

  // AAM - 乘法后的ASCII调整
  private executeAam(instruction: DecodedInstruction): void {
    // 获取AL寄存器的值（低8位）
    let al = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    let ah = (this.registerManager.getRegister(Registers.RAX) >> 8n) & 0xFFn;

    // AAM指令有一个隐含的立即数操作数，通常是10（十进制调整）
    // 在实际x86中，这个值从指令操作码中获取
    // 这里假设instruction.immediate包含了这个值
    const imm = instruction.immediate !== undefined ? BigInt(instruction.immediate) : 10n;

    // 检查除数是否为0
    if (imm === 0n) {
      this.raiseDivideError();
      return;
    }

    // 执行AAM操作：al = al / imm（商），ah = al % imm（余数）
    ah = al / imm;
    al = al % imm;

    // 更新AX寄存器
    this.registerManager.setRegister(Registers.RAX, (ah << 8n) | al);

    // 更新标志位
    this.updateFlagsForAam(al);
  }

  // 更新AAM指令的标志位
  private updateFlagsForAam(value: u64): void {
    // CF和AF标志在AAM指令中设置为0
    this.registerManager.setFlag('CF', false);
    this.registerManager.setFlag('AF', false);

    // ZF - 如果结果为零则设置
    this.registerManager.setFlag('ZF', value === 0n);

    // SF - 结果的最高位
    this.registerManager.setFlag('SF', Number(value) < 0);

    // PF - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(value);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // OF标志未定义
  }

  // AAD - 除法前的ASCII调整
  private executeAad(instruction: DecodedInstruction): void {
    // 获取AL和AH寄存器的值（低16位）
    let al = this.registerManager.getRegister(Registers.RAX) & 0xFFn;
    let ah = (this.registerManager.getRegister(Registers.RAX) >> 8n) & 0xFFn;

    // AAD指令有一个隐含的立即数操作数，通常是10（十进制调整）
    // 在实际x86中，这个值从指令操作码中获取
    // 这里假设instruction.immediate包含了这个值
    const imm = instruction.immediate !== undefined ? BigInt(instruction.immediate) : 10n;

    // 执行AAD操作：AL = (AH * imm + AL) & 0xFFn（限制为8位）
    // 然后AH被清零
    al = (ah * imm + al) & 0xFFn;
    ah = 0n;

    // 更新AX寄存器
    this.registerManager.setRegister(Registers.RAX, (ah << 8n) | al);

    // 更新标志位
    this.updateFlagsForAad(al);
  }

  // 更新AAD指令的标志位
  private updateFlagsForAad(value: u64): void {
    // CF和AF标志在AAD指令中设置为0
    this.registerManager.setFlag('CF', false);
    this.registerManager.setFlag('AF', false);

    // ZF - 如果结果为零则设置
    this.registerManager.setFlag('ZF', value === 0n);

    // SF - 结果的最高位
    this.registerManager.setFlag('SF', Number(value) < 0);

    // PF - 结果中1的个数是否为偶数
    const bitCount = this.countSetBits(value);
    this.registerManager.setFlag('PF', bitCount % 2 === 0);

    // OF标志未定义
  }

  // =============================================================================
  // 中断和异常指令实现
  // =============================================================================

  // INT - 中断指令
  private executeInt(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const interruptNumber = instruction.immediate;

    // 保存指令指针
    const ip = this.registerManager.getInstructionPointer();

    // 压入标志寄存器和返回地址（简化实现）
    const flags = 0n; // 简化处理，实际应该获取标志寄存器的值
    this.push(ip);
    this.push(flags);

    // 禁用中断
    this.registerManager.setFlag('IF', false);

    // 模拟中断处理程序地址
    const interruptHandlerAddress = BigInt(0x10000); // 简化处理

    // 跳转到中断处理程序
    this.registerManager.setInstructionPointer(interruptHandlerAddress);
  }

  // INTO - 溢出时中断
  private executeInto(): void {
    // 如果OF标志为1，则触发INT 4中断
    if (this.registerManager.getFlag('OF')) {
      this.executeInt({ immediate: 4n } as DecodedInstruction);
    }
  }

  /**
   * ENTER - 过程进入指令
   * 为局部变量分配栈空间并设置帧指针
   * @param localVarSize 局部变量所需的字节数
   * @param nestingLevel 嵌套级别（栈帧数）
   */
  private executeEnter(localVarSize: number, nestingLevel: number): void {
    // 保存当前RBP寄存器值到栈
    const currentBP = this.registerManager.getRegister(Registers.RBP);
    this.push(currentBP);

    // 设置新的RBP值等于当前RSP值
    const currentSP = this.registerManager.getRegister(Registers.RSP);
    this.registerManager.setRegister(Registers.RBP, currentSP);

    // 为局部变量分配栈空间（减去局部变量大小）
    if (localVarSize > 0) {
      this.registerManager.setRegister(
        Registers.RSP,
        currentSP - BigInt(localVarSize)
      );
    }

    // 处理嵌套级别（如果大于0）
    // 实际的ENTER指令会根据嵌套级别复制之前的BP值到栈上
    // 这里简化实现，主要处理基本的栈帧设置
  }

  /**
   * LEAVE - 过程离开指令
   * 释放局部变量栈空间并恢复前一帧指针
   */
  private executeLeave(): void {
    // 获取当前RBP值（即当前栈帧的基址）
    const currentBP = this.registerManager.getRegister(Registers.RBP);

    // 恢复RSP值到RBP值（释放局部变量空间）
    this.registerManager.setRegister(Registers.RSP, currentBP);

    // 弹出前一帧的RBP值并恢复到RBP寄存器
    const previousBP = this.pop();
    this.registerManager.setRegister(Registers.RBP, previousBP);
  }

  /**
   * BOUND - 边界检查指令
   * 检查操作数是否在指定的边界范围内
   * @param target 要检查的目标值
   * @param lowerBound 下界
   * @param upperBound 上界
   */
  private executeBound(target: bigint, lowerBound: bigint, upperBound: bigint): void {
    // 检查目标值是否小于下界或大于上界
    if (target < lowerBound || target > upperBound) {
      // 如果超出边界，触发#BR异常（边界检查异常）
      // 在实际的CPU中，这会触发中断4
      // 这里简化实现，抛出一个错误
      throw new Error(`BOUND instruction: value ${target} out of bounds [${lowerBound}, ${upperBound}]`);
    }
    // 如果在边界内，则继续执行
  }

  /**
   * IRET/IRETD/IRETQ - 中断返回指令
   * 根据当前机器模式执行相应的中断返回操作
   * - IRETQ: 64位模式下执行，从栈中弹出RFLAGS, CS, RIP
   * - IRETD: 32位保护模式下执行，从栈中弹出EFLAGS, CS, EIP
   * - IRET: 16位实模式下执行，从栈中弹出FLAGS, CS, IP
   */
  private executeIret(): void {
    switch (this.mode) {
      case MachineMode.LONG:
        // 64位模式 - IRETQ指令
        // 弹出返回地址 (RIP)
        const returnAddress64 = this.pop();

        // 弹出代码段选择器 (CS)
        const csSelector64 = this.pop();

        // 弹出标志寄存器 (RFLAGS)
        const flags64 = this.pop();
        this.registerManager.setFlagsRegister(flags64);

        // 跳转到返回地址
        this.registerManager.setInstructionPointer(returnAddress64);

        break;

      case MachineMode.LEGACY:
        // 32位保护模式 - IRETD指令
        // 弹出返回地址 (EIP)
        const returnAddress32 = this.pop() & 0xFFFFFFFFn;

        // 弹出代码段选择器 (CS)
        const csSelector32 = this.pop() & 0xFFFFn;

        // 弹出标志寄存器 (EFLAGS)
        const flags32 = this.pop() & 0xFFFFFFFFn;
        this.registerManager.setFlagsRegister(flags32);

        // 跳转到返回地址
        this.registerManager.setInstructionPointer(returnAddress32);

        break;

      case MachineMode.REAL:
      default:
        // 16位实模式 - IRET指令
        // 弹出返回地址 (IP)
        const returnAddress16 = this.pop() & 0xFFFFn;

        // 弹出代码段选择器 (CS)
        const csSelector16 = this.pop() & 0xFFFFn;

        // 弹出标志寄存器 (FLAGS)
        const flags16 = this.pop() & 0xFFFFn;
        this.registerManager.setFlagsRegister(flags16);

        // 跳转到返回地址
        this.registerManager.setInstructionPointer(returnAddress16);

        break;
    }

    // 恢复中断标志（根据标志寄存器中的值）
    // 实际应该根据弹出的标志值设置IF，但为了简化，这里保持原逻辑
    this.registerManager.setFlag('IF', true);
  }

  // INT3 - 断点中断
  private executeInt3(): void {
    // INT3是特殊的单字节中断指令，操作码为0xCC
    // 通常用于调试断点
    this.executeInt({ immediate: 3n } as DecodedInstruction);
  }

  // UD2 - 未定义指令
  private executeUd2(): void {
    // 抛出未定义指令异常
    throw new Error('Undefined instruction: UD2');
  }

  // =============================================================================
  // 内存和缓存控制指令实现
  // =============================================================================

  // MFENCE - 内存屏障
  private executeMfence(): void {
    // 在JavaScript中，我们无法直接控制内存屏障，但可以记录该指令的执行
    // 实际的硬件级内存屏障在JS中无法实现，这里仅作为占位符
    console.log('MFENCE instruction executed');
  }

  // LFENCE - 加载屏障
  private executeLfence(): void {
    // 加载屏障的JavaScript模拟实现
    console.log('LFENCE instruction executed');
  }

  // SFENCE - 存储屏障
  private executeSfence(): void {
    // 存储屏障的JavaScript模拟实现
    console.log('SFENCE instruction executed');
  }

  // 此函数已在文件后面实现，此处为重复定义，已移除

  // CLFLUSH - 缓存行刷新
  private executeClflush(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 在JavaScript中模拟缓存行刷新
    console.log(`CLFLUSH instruction executed on address: 0x${address.toString(16)}`);
  }

  // =============================================================================
  // 系统和特权指令实现
  // =============================================================================

  // HLT - 停机指令
  private executeHlt(): void {
    // 在JavaScript中模拟处理器停机
    console.log('HLT instruction executed - processor halted');
    // 实际实现中可能需要暂停执行循环
  }

  // RDTSC - 读取时间戳计数器
  private executeRdtsc(): void {
    // 获取当前的高精度时间戳
    const timestamp = BigInt(Date.now() * 1000); // 以微秒为单位

    // 将时间戳分为两部分存入EDX:EAX
    const eax = timestamp & 0xFFFFFFFFn;
    const edx = (timestamp >> 32n) & 0xFFFFFFFFn;

    // 更新寄存器
    this.registerManager.setRegister(Registers.RAX, eax);
    this.registerManager.setRegister(Registers.RDX, edx);
  }

  // RDTSCP - 读取时间戳计数器和处理器ID
  private executeRdtscp(): void {
    // 获取当前的高精度时间戳
    const timestamp = BigInt(Date.now() * 1000); // 以微秒为单位

    // 将时间戳分为两部分存入EDX:EAX
    const eax = timestamp & 0xFFFFFFFFn;
    const edx = (timestamp >> 32n) & 0xFFFFFFFFn;

    // 假设的处理器ID，实际实现中应该读取真实的处理器ID
    const ecx = 0n; // 模拟处理器ID

    // 更新寄存器
    this.registerManager.setRegister(Registers.RAX, eax);
    this.registerManager.setRegister(Registers.RDX, edx);
    this.registerManager.setRegister(Registers.RCX, ecx);
  }

  // RDMSR - 读取模型特定寄存器
  private executeRdmsr(): void {
    // 从ECX寄存器获取MSR地址
    const msrAddress = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;

    // 根据MSR地址返回适当的值（模拟实现）
    let eaxValue = 0n;
    let edxValue = 0n;

    switch (msrAddress) {
      case 0xC0000080n: // IA32_EFER - 扩展特性启用寄存器
        // 模拟支持SCE (System Call Extensions) 和 NXE (No-Execute Enable)
        eaxValue = 0x00000001n; // SCE位设置
        edxValue = 0x00000000n;
        break;
      case 0xC0000081n: // IA32_STAR - 系统调用目标操作数寄存器
        // 模拟值
        eaxValue = 0x00180008n;
        edxValue = 0x00000000n;
        break;
      case 0xC0000082n: // IA32_LSTAR - 长模式系统调用目标地址
        // 模拟值
        eaxValue = 0x00000000n;
        edxValue = 0x00000000n;
        break;
      case 0xC0000083n: // IA32_FMASK - 系统调用标志掩码
        // 模拟值
        eaxValue = 0x00000000n;
        edxValue = 0x00000000n;
        break;
      default:
        // 对于未知的MSR地址，返回0
        eaxValue = 0n;
        edxValue = 0n;
    }

    // 更新EAX和EDX寄存器
    this.registerManager.setRegister(Registers.RAX, eaxValue);
    this.registerManager.setRegister(Registers.RDX, edxValue);
  }

  // CPUID - 处理器特性识别指令
  private executeCpuid(): void {
    // 获取EAX寄存器的值（功能号）
    const eax = this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn;

    // 根据功能号设置返回值
    switch (eax) {
      case 0n: {
        // 基本功能查询，返回厂商信息和最大功能号
        // 最大功能号
        this.registerManager.setRegister(Registers.RAX, 0x0000000Dn);
        // 厂商信息：GenuineIntel
        this.registerManager.setRegister(Registers.RBX, 0x756E6547n); // 'uneG'
        this.registerManager.setRegister(Registers.RDX, 0x49656E69n); // 'Ieni'
        this.registerManager.setRegister(Registers.RCX, 0x6C65746En); // 'letn'
        break;
      }
      case 1n: {
        // 返回处理器特性标志
        // EAX: 步进、型号、家族信息
        this.registerManager.setRegister(Registers.RAX, 0x000306A9n);
        // EBX: 缓存信息
        this.registerManager.setRegister(Registers.RBX, 0x00000800n);
        // ECX: 特性标志（SSE3, SSSE3, SSE4.1, SSE4.2, AVX等）
        this.registerManager.setRegister(Registers.RCX, 0x7FFAFBBF0n);
        // EDX: 特性标志（MMX, SSE, SSE2等）
        this.registerManager.setRegister(Registers.RDX, 0xBFEBFBFFn);
        break;
      }
      case 2n: {
        // CPU缓存和TLB信息
        this.registerManager.setRegister(Registers.RAX, 0x65535043n); // 'ESP'
        this.registerManager.setRegister(Registers.RBX, 0x20202020n); // '    '
        this.registerManager.setRegister(Registers.RCX, 0x00746E6Fn); // 'otn'
        this.registerManager.setRegister(Registers.RDX, 0x0065746En); // 'ten'
        break;
      }
      case 4n: {
        // 缓存参数
        this.registerManager.setRegister(Registers.RAX, 0x00000000n);
        this.registerManager.setRegister(Registers.RBX, 0x00000000n);
        this.registerManager.setRegister(Registers.RCX, 0x00000000n);
        this.registerManager.setRegister(Registers.RDX, 0x00000000n);
        break;
      }
      case 6n: {
        // 性能和电源管理特性
        this.registerManager.setRegister(Registers.RAX, 0x00000003n);
        this.registerManager.setRegister(Registers.RBX, 0x00000000n);
        this.registerManager.setRegister(Registers.RCX, 0x00000003n);
        this.registerManager.setRegister(Registers.RDX, 0x00000000n);
        break;
      }
      case 0x0Dn: {
        // XSAVE功能
        const ecx = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;
        if (ecx === 0n) {
          // 支持的XCR0位
          this.registerManager.setRegister(Registers.RAX, 0x00000003n);
          this.registerManager.setRegister(Registers.RBX, 0x00000000n);
          this.registerManager.setRegister(Registers.RCX, 0x00000000n);
          this.registerManager.setRegister(Registers.RDX, 0x00000000n);
        }
        break;
      }
      default: {
        // 未实现的功能号，返回全0
        this.registerManager.setRegister(Registers.RAX, 0n);
        this.registerManager.setRegister(Registers.RBX, 0n);
        this.registerManager.setRegister(Registers.RCX, 0n);
        this.registerManager.setRegister(Registers.RDX, 0n);
        break;
      }
    }
  }

  // =============================================================================
  // 段寄存器操作指令实现
  // =============================================================================

  // LDS - 加载数据段寄存器
  private executeLds(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 读取偏移量（双字）
    const offset = this.readDwordFromMemory(address);
    // 读取段选择器（字）
    const segmentSelector = this.readWordFromMemory(address + 4n);

    // 设置通用寄存器（目标寄存器）
    this.registerManager.setRegister(instruction.modrm.reg, offset);
    // 设置数据段寄存器
    this.registerManager.setSegmentRegister(SegmentRegisters.DS, Number(segmentSelector));
  }

  // LES - 加载扩展段寄存器
  private executeLes(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 读取偏移量（双字）
    const offset = this.readDwordFromMemory(address);
    // 读取段选择器（字）
    const segmentSelector = this.readWordFromMemory(address + 4n);

    // 设置通用寄存器（目标寄存器）
    this.registerManager.setRegister(instruction.modrm.reg, offset);
    // 设置扩展段寄存器
    this.registerManager.setSegmentRegister(SegmentRegisters.ES, Number(segmentSelector));
  }

  // LFS - 加载F段寄存器
  private executeLfs(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 读取偏移量（双字）
    const offset = this.readDwordFromMemory(address);
    // 读取段选择器（字）
    const segmentSelector = this.readWordFromMemory(address + 4n);

    // 设置通用寄存器（目标寄存器）
    this.registerManager.setRegister(instruction.modrm.reg, offset);
    // 设置F段寄存器
    this.registerManager.setSegmentRegister(SegmentRegisters.FS, Number(segmentSelector));
  }

  // LGS - 加载G段寄存器
  private executeLgs(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 读取偏移量（双字）
    const offset = this.readDwordFromMemory(address);
    // 读取段选择器（字）
    const segmentSelector = this.readWordFromMemory(address + 4n);

    // 设置通用寄存器（目标寄存器）
    this.registerManager.setRegister(instruction.modrm.reg, offset);
    // 设置G段寄存器
    this.registerManager.setSegmentRegister(SegmentRegisters.GS, Number(segmentSelector));
  }

  // LSS - 加载堆栈段寄存器
  private executeLss(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 读取偏移量（双字）
    const offset = this.readDwordFromMemory(address);
    // 读取段选择器（字）
    const segmentSelector = this.readWordFromMemory(address + 4n);

    // 设置通用寄存器（目标寄存器）
    this.registerManager.setRegister(instruction.modrm.reg, offset);
    // 设置堆栈段寄存器
    this.registerManager.setSegmentRegister(SegmentRegisters.SS, Number(segmentSelector));
  }

  // LSL - 加载段界限
  private executeLsl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取段选择器
    let selector: u64;
    if (instruction.modrm.mod === 3) {
      // 段选择器在寄存器中
      selector = this.registerManager.getRegister(instruction.modrm.rm);
    } else {
      // 段选择器在内存中
      const addr = this.calculateEffectiveAddress(instruction);
      selector = this.readQwordFromMemory(addr);
    }

    // 检查选择器是否有效（这里简化处理）
    if ((selector & 1n) === 0n) {
      // 选择器无效
      this.registerManager.setFlag('ZF', true);
      return;
    }

    // 模拟获取段界限（实际实现中应该从GDT或LDT中获取）
    let limit = 0xFFFFFFFFn; // 假设的段界限

    // 写入目标寄存器
    this.registerManager.setRegister(instruction.modrm.reg, limit);
    this.registerManager.setFlag('ZF', false);
  }

  // XLAT - 换码指令
  private executeXlat(): void {
    // 获取BX寄存器的值作为表基址
    const tableBase = this.registerManager.getRegister(Registers.RBX);

    // 获取AL寄存器的值作为索引
    const index = this.registerManager.getRegister(Registers.RAX) & 0xFFn;

    // 计算表项地址
    const tableEntryAddress = tableBase + index;

    // 读取表项值并写入AL寄存器
    const tableEntryValue = this.readByteFromMemory(tableEntryAddress);
    const newRax = (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFFFFFFF00n) | BigInt(tableEntryValue);
    this.registerManager.setRegister(Registers.RAX, newRax);
  }

  // =============================================================================
  // 比较并交换指令实现
  // =============================================================================

  // CMPXCHG8B - 比较并交换8字节
  private executeCmpXchg8b(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 读取内存中的值
    const memoryValue = this.readQwordFromMemory(address);

    // 获取EDX:EAX中的值作为比较值
    const compareValue = (this.registerManager.getRegister(Registers.RDX) << 32n) |
      (this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn);

    // 比较
    if (memoryValue === compareValue) {
      // 相等，将ECX:EBX写入内存
      const storeValue = (this.registerManager.getRegister(Registers.RCX) << 32n) |
        (this.registerManager.getRegister(Registers.RBX) & 0xFFFFFFFFn);
      this.writeQwordToMemory(address, storeValue);
      this.registerManager.setFlag('ZF', true);
    } else {
      // 不相等，将内存值加载到EDX:EAX
      this.registerManager.setRegister(Registers.RAX, memoryValue & 0xFFFFFFFFn);
      this.registerManager.setRegister(Registers.RDX, memoryValue >> 32n);
      this.registerManager.setFlag('ZF', false);
    }
  }

  // CMPXCHG16B - 比较并交换16字节
  private executeCmpXchg16b(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 读取内存中的16字节值
    const memoryValueLo = this.readQwordFromMemory(address);
    const memoryValueHi = this.readQwordFromMemory(address + 8n);

    // 获取RDX:RAX中的值作为比较值
    const compareValueLo = this.registerManager.getRegister(Registers.RAX);
    const compareValueHi = this.registerManager.getRegister(Registers.RDX);

    // 比较
    if (memoryValueLo === compareValueLo && memoryValueHi === compareValueHi) {
      // 相等，将RCX:RBX写入内存
      this.writeQwordToMemory(address, this.registerManager.getRegister(Registers.RBX));
      this.writeQwordToMemory(address + 8n, this.registerManager.getRegister(Registers.RCX));
      this.registerManager.setFlag('ZF', true);
    } else {
      // 不相等，将内存值加载到RDX:RAX
      this.registerManager.setRegister(Registers.RAX, memoryValueLo);
      this.registerManager.setRegister(Registers.RDX, memoryValueHi);
      this.registerManager.setFlag('ZF', false);
    }
  }

  // =============================================================================
  // 系统调用指令实现
  // =============================================================================

  // SYSCALL - 系统调用指令
  private executeSyscall(): void {
    // 保存返回地址到RCX
    const returnRip = this.registerManager.getInstructionPointer();
    this.registerManager.setRegister(Registers.RCX, returnRip);

    // 禁用IF标志
    this.registerManager.setFlag('IF', false);

    // 模拟系统调用处理程序地址
    const syscallHandler = BigInt(0x20000); // 简化处理

    // 跳转到系统调用处理程序
    this.registerManager.setInstructionPointer(syscallHandler);
  }

  // SYSRET - 系统调用返回指令
  private executeSysret(): void {
    // 从RCX恢复返回地址
    const returnRip = this.registerManager.getRegister(Registers.RCX);
    this.registerManager.setInstructionPointer(returnRip);

    // 恢复IF标志
    this.registerManager.setFlag('IF', true);
  }


  // =============================================================================
  // 输入输出指令实现
  // =============================================================================






  // 辅助方法：从I/O端口读取一个字节
  private portReadByte(port: u64): number {
    // 在JavaScript中模拟I/O端口读取
    console.log(`Reading byte from I/O port: 0x${port.toString(16)}`);
    // 实际实现中应该与硬件或模拟器交互
    return 0; // 返回默认值
  }

  // 辅助方法：从I/O端口读取一个字
  private portReadWord(port: u64): u16 {
    // 在JavaScript中模拟I/O端口读取
    console.log(`Reading word from I/O port: 0x${port.toString(16)}`);
    // 实际实现中应该与硬件或模拟器交互
    return 0; // 返回默认值
  }

  // 辅助方法：从I/O端口读取一个双字
  private portReadDword(port: u64): u64 {
    // 在JavaScript中模拟I/O端口读取
    console.log(`Reading dword from I/O port: 0x${port.toString(16)}`);
    // 实际实现中应该与硬件或模拟器交互
    return 0n; // 返回默认值
  }

  // 辅助方法：写入一个字节到I/O端口
  private portWriteByte(port: u64, value: number): void {
    // 在JavaScript中模拟I/O端口写入
    console.log(`Writing byte 0x${value.toString(16)} to I/O port: 0x${port.toString(16)}`);
  }

  // 辅助方法：写入一个字到I/O端口
  private portWriteWord(port: u64, value: u16): void {
    // 在JavaScript中模拟I/O端口写入
    console.log(`Writing word 0x${value.toString(16)} to I/O port: 0x${port.toString(16)}`);
  }

  // 辅助方法：写入一个双字到I/O端口
  private portWriteDword(port: u64, value: u64): void {
    // 在JavaScript中模拟I/O端口写入
    console.log(`Writing dword 0x${value.toString(16)} to I/O port: 0x${port.toString(16)}`);
  }

  // =============================================================================
  // 特殊指令实现
  // =============================================================================

  // RDRAND - 读取随机数指令
  private executeRdrand(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 生成随机数（在实际实现中应该使用硬件随机数生成器）
    const randomValue = BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER));

    // 写入目标寄存器
    this.registerManager.setRegister(instruction.modrm.rm, randomValue);

    // 设置CF标志表示成功
    this.registerManager.setFlag('CF', true);
  }

  // RDSEED - 读取种子指令
  private executeRdseed(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 生成种子值（在实际实现中应该使用硬件随机数生成器）
    const seedValue = BigInt(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER));

    // 写入目标寄存器
    this.registerManager.setRegister(instruction.modrm.rm, seedValue);

    // 设置CF标志表示成功
    this.registerManager.setFlag('CF', true);
  }

  // LOCK - 总线锁定前缀
  private executeLock(instruction: DecodedInstruction): void {
    // LOCK前缀是一个指令前缀，用于锁定总线以进行原子操作
    // 在JavaScript中，我们可以标记该指令需要原子执行
    console.log('LOCK prefix applied to instruction');
    // 注意：实际执行原子操作需要进一步的实现
  }

  // REP/REPE/REPNE - 重复前缀
  private executeRep(instruction: DecodedInstruction): void {
    // 重复前缀用于重复执行后续的字符串指令
    // 获取RCX寄存器中的重复计数
    let repeatCount = this.registerManager.getRegister(Registers.RCX);

    // 获取前缀类型
    const hasRepPrefix = instruction.prefixes.has(PrefixType.REP);
    const hasRepnePrefix = instruction.prefixes.has(PrefixType.REPNE);

    // 保存当前的指令指针，以便后续执行
    const originalRip = this.registerManager.getInstructionPointer();

    // 根据指令操作码确定要重复执行的字符串指令
    let stringInstruction: DecodedInstruction = {
      prefixes: new Set(), // 不包含重复前缀
      opcode: instruction.opcode,
      modrm: instruction.modrm,
      immediate: instruction.immediate,
      displacement: instruction.displacement,
      size: instruction.size
    };

    // 处理不同类型的重复前缀和字符串指令组合
    while (repeatCount > 0n) {
      // 执行相应的字符串指令
      switch (instruction.opcode) {
        case 0xA4: // MOVSB
          this.executeMovsb(stringInstruction);
          // REP MOVSB 无条件重复，只检查RCX
          repeatCount--;
          break;

        case 0xA5: // MOVS
          this.executeMovs(stringInstruction);
          // REP MOVS 无条件重复，只检查RCX
          repeatCount--;
          break;

        case 0xAA: // STOSB
          this.executeStosb(stringInstruction);
          // REP STOSB 无条件重复，只检查RCX
          repeatCount--;
          break;

        case 0xAB: // STOS
          this.executeStos(stringInstruction);
          // REP STOS 无条件重复，只检查RCX
          repeatCount--;
          break;

        case 0xAC: // LODSB
          this.executeLodsb(stringInstruction);
          // REP LODSB 无条件重复，只检查RCX
          repeatCount--;
          break;

        case 0xAD: // LODS
          this.executeLods(stringInstruction);
          // REP LODS 无条件重复，只检查RCX
          repeatCount--;
          break;

        case 0xA6: // CMPSB
          this.executeCmpsb(stringInstruction);
          // 根据前缀类型决定停止条件
          if (hasRepPrefix) {
            // REPE/REPZ CMPSB - 当ZF=0或RCX=0时停止
            if (!this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=0，提前停止
            } else {
              repeatCount--;
            }
          } else if (hasRepnePrefix) {
            // REPNE/REPNZ CMPSB - 当ZF=1或RCX=0时停止
            if (this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=1，提前停止
            } else {
              repeatCount--;
            }
          } else {
            // 普通REP CMPSB - 只检查RCX
            repeatCount--;
          }
          break;

        case 0xA7: // CMPS
          this.executeCmps(stringInstruction);
          // 根据前缀类型决定停止条件
          if (hasRepPrefix) {
            // REPE/REPZ CMPS - 当ZF=0或RCX=0时停止
            if (!this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=0，提前停止
            } else {
              repeatCount--;
            }
          } else if (hasRepnePrefix) {
            // REPNE/REPNZ CMPS - 当ZF=1或RCX=0时停止
            if (this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=1，提前停止
            } else {
              repeatCount--;
            }
          } else {
            // 普通REP CMPS - 只检查RCX
            repeatCount--;
          }
          break;

        case 0xAE: // SCASB
          this.executeScasb(stringInstruction);
          // 根据前缀类型决定停止条件
          if (hasRepPrefix) {
            // REPE/REPZ SCASB - 当ZF=0或RCX=0时停止
            if (!this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=0，提前停止
            } else {
              repeatCount--;
            }
          } else if (hasRepnePrefix) {
            // REPNE/REPNZ SCASB - 当ZF=1或RCX=0时停止
            if (this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=1，提前停止
            } else {
              repeatCount--;
            }
          } else {
            // 普通REP SCASB - 只检查RCX
            repeatCount--;
          }
          break;

        case 0xAF: // SCAS
          this.executeScas(stringInstruction);
          // 根据前缀类型决定停止条件
          if (hasRepPrefix) {
            // REPE/REPZ SCAS - 当ZF=0或RCX=0时停止
            if (!this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=0，提前停止
            } else {
              repeatCount--;
            }
          } else if (hasRepnePrefix) {
            // REPNE/REPNZ SCAS - 当ZF=1或RCX=0时停止
            if (this.registerManager.getFlag('ZF')) {
              repeatCount = 0n; // ZF=1，提前停止
            } else {
              repeatCount--;
            }
          } else {
            // 普通REP SCAS - 只检查RCX
            repeatCount--;
          }
          break;

        default:
          // 不支持的字符串指令，退出循环
          repeatCount = 0n;
          break;
      }
    }

    // 更新RCX寄存器的值（实际x86中REP会递减RCX，但这里我们已经在循环中处理了）
    this.registerManager.setRegister(Registers.RCX, 0n);
  }

  // STAC - 启用阴影堆栈
  private executeStac(): void {
    // 启用阴影堆栈保护
    console.log('STAC instruction executed - Shadow Stack enabled');
    // 实际实现中应该设置相应的控制位
  }

  // CLAC - 禁用阴影堆栈
  private executeClac(): void {
    // 禁用阴影堆栈保护
    console.log('CLAC instruction executed - Shadow Stack disabled');
    // 实际实现中应该清除相应的控制位
  }

  // INVD - 使高速缓存无效
  private executeInvd(): void {
    // 在JavaScript中模拟缓存无效化操作
    console.log('INVD instruction executed');
  }

  // WBINVD - 使高速缓存无效并回写
  private executeWbinvd(): void {
    // 在JavaScript中模拟缓存无效化并回写操作
    console.log('WBINVD instruction executed');
  }

  // INVLPG - 使TLB中指定页表项无效
  private executeInvlpg(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);
    console.log(`INVLPG instruction executed on address: 0x${address.toString(16)}`);
    // 实际实现中应该使对应的页表项无效
  }

  // IN - 从端口输入指令（立即数端口号，8位数据）
  private executeInImm8Al(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const port = instruction.immediate;
    const value = this.portReadByte(port);

    // 设置AL寄存器（RAX的低8位）
    const raxValue = this.registerManager.getRegister(Registers.RAX);
    const newRaxValue = (raxValue & 0xFFFFFFFFFFFFFF00n) | BigInt(value & 0xFF);
    this.registerManager.setRegister(Registers.RAX, newRaxValue);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // IN - 从端口输入指令（立即数端口号，16位数据）
  private executeInImm8Ax(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const port = instruction.immediate;
    const value = this.portReadWord(port);

    // 设置AX寄存器（RAX的低16位）
    const raxValue = this.registerManager.getRegister(Registers.RAX);
    const newRaxValue = (raxValue & 0xFFFFFFFFFFFF0000n) | BigInt(value & 0xFFFF);
    this.registerManager.setRegister(Registers.RAX, newRaxValue);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // IN - 从端口输入指令（DX寄存器端口号，8位数据）
  private executeInDxAl(instruction: DecodedInstruction): void {
    const port = this.registerManager.getRegister(Registers.RDX);
    const value = this.portReadByte(port);

    // 设置AL寄存器（RAX的低8位）
    const raxValue = this.registerManager.getRegister(Registers.RAX);
    const newRaxValue = (raxValue & 0xFFFFFFFFFFFFFF00n) | BigInt(value & 0xFF);
    this.registerManager.setRegister(Registers.RAX, newRaxValue);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // IN - 从端口输入指令（DX寄存器端口号，16位数据）
  private executeInDxAx(instruction: DecodedInstruction): void {
    const port = this.registerManager.getRegister(Registers.RDX);
    const value = this.portReadWord(port);

    // 设置AX寄存器（RAX的低16位）
    const raxValue = this.registerManager.getRegister(Registers.RAX);
    const newRaxValue = (raxValue & 0xFFFFFFFFFFFF0000n) | BigInt(value & 0xFFFF);
    this.registerManager.setRegister(Registers.RAX, newRaxValue);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // IN - 从端口输入指令（DX寄存器端口号，32位数据）
  private executeInDxEax(instruction: DecodedInstruction): void {
    const port = this.registerManager.getRegister(Registers.RDX);
    const value = this.portReadDword(port);

    // 设置EAX寄存器（RAX的低32位）
    const raxValue = this.registerManager.getRegister(Registers.RAX);
    const newRaxValue = (raxValue & 0xFFFFFFFF00000000n) | (value & 0xFFFFFFFFn);
    this.registerManager.setRegister(Registers.RAX, newRaxValue);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // OUT - 输出到端口指令（立即数端口号，8位数据）
  private executeOutImm8Al(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const port = instruction.immediate;
    const value = Number(this.registerManager.getRegister(Registers.RAX) & 0xFFn);

    this.portWriteByte(port, value);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // OUT - 输出到端口指令（立即数端口号，16位数据）
  private executeOutImm8Ax(instruction: DecodedInstruction): void {
    if (instruction.immediate === undefined) return;

    const port = instruction.immediate;
    const value = Number(this.registerManager.getRegister(Registers.RAX) & 0xFFFFn);

    this.portWriteWord(port, value);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // OUT - 输出到端口指令（DX寄存器端口号，8位数据）
  private executeOutDxAl(instruction: DecodedInstruction): void {
    const port = this.registerManager.getRegister(Registers.RDX);
    const value = Number(this.registerManager.getRegister(Registers.RAX) & 0xFFn);

    this.portWriteByte(port, value);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // OUT - 输出到端口指令（DX寄存器端口号，16位数据）
  private executeOutDxAx(instruction: DecodedInstruction): void {
    const port = this.registerManager.getRegister(Registers.RDX);
    const value = Number(this.registerManager.getRegister(Registers.RAX) & 0xFFFFn);

    this.portWriteWord(port, value);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // OUT - 输出到端口指令（DX寄存器端口号，32位数据）
  private executeOutDxEax(instruction: DecodedInstruction): void {
    const port = this.registerManager.getRegister(Registers.RDX);
    const value = this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn;

    this.portWriteDword(port, value);

    this.registerManager.setInstructionPointer(this.registerManager.getInstructionPointer() + BigInt(instruction.size));
  }

  // PAUSE - 暂停指令
  private executePause(): void {
    // PAUSE指令用于自旋等待循环中
    // 在JavaScript中，我们可以模拟一个短暂的延迟
    console.log('PAUSE instruction executed');
    // 实际实现中可能需要调用某种延迟或让出CPU的方法
  }

  // FWAIT/WAIT - 等待指令
  private executeFwait(): void {
    // FWAIT指令使处理器等待直到所有未完成的浮点指令执行完毕
    console.log('FWAIT instruction executed');
    // 实际实现中应该检查浮点指令队列的状态
  }

  // EMMS - 清空多媒体状态指令
  private executeEmms(): void {
    // EMMS指令将MMX状态清空为x87 FPU状态
    console.log('EMMS instruction executed');
    // 实际实现中应该重置MMX状态
  }

  // COMISS - 比较标量单精度浮点数并设置标志
  private executeComiss(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, 4); // 单精度浮点数是4字节
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);

    // 读取单精度浮点数（只取低4字节）
    const destFloat = this.readFloatFromUint8Array(destData, 0);
    const srcFloat = this.readFloatFromUint8Array(srcData, 0);

    // 设置ZF, PF, CF标志
    this.registerManager.setFlag('ZF', destFloat === srcFloat);
    this.registerManager.setFlag('PF', isNaN(destFloat) || isNaN(srcFloat));
    this.registerManager.setFlag('CF', destFloat < srcFloat);

    // 清除OF, AF, SF标志
    this.registerManager.setFlag('OF', false);
    this.registerManager.setFlag('AF', false);
    this.registerManager.setFlag('SF', false);
  }

  // DIVPD - 双精度浮点数除法
  private executeDivpd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 执行双精度浮点数除法（两个64位浮点数）
    for (let i = 0; i < 2; i++) {
      // 读取双精度浮点数
      const destFloat = this.readDoubleFromUint8Array(destData, i * 8);
      const srcFloat = this.readDoubleFromUint8Array(srcData, i * 8);

      // 执行除法
      const resultFloat = destFloat / srcFloat;

      // 写入结果
      this.writeDoubleToUint8Array(resultData, i * 8, resultFloat);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // DIVPS - 单精度浮点数除法
  private executeDivps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 执行单精度浮点数除法（四个32位浮点数）
    for (let i = 0; i < 4; i++) {
      // 读取单精度浮点数
      const destFloat = this.readFloatFromUint8Array(destData, i * 4);
      const srcFloat = this.readFloatFromUint8Array(srcData, i * 4);

      // 执行除法
      const resultFloat = destFloat / srcFloat;

      // 写入结果
      this.writeFloatToUint8Array(resultData, i * 4, resultFloat);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // INS - 从I/O端口输入到内存
  private executeIns(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 获取目标内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 获取端口号（从DX寄存器）
    const port = this.registerManager.getRegister(Registers.RDX);

    // 根据操作数大小决定传输的数据大小
    let dataSize = 1; // 默认字节
    if (instruction.prefixes.has(PrefixType.OPERAND_SIZE)) {
      dataSize = 2; // 字
    }
    if (instruction.prefixes.has(PrefixType.ADDRESS_SIZE)) {
      dataSize = 4; // 双字
    }

    // 从端口读取数据
    let data: number;
    if (dataSize === 1) {
      data = this.portReadByte(port);
    } else if (dataSize === 2) {
      data = Number(this.portReadWord(port));
    } else {
      data = Number(this.portReadDword(port));
    }

    // 写入内存
    if (dataSize === 1) {
      this.writeByteToMemory(address, data);
    } else if (dataSize === 2) {
      this.writeWordToMemory(address, data);
    } else {
      this.writeDwordToMemory(address, BigInt(data));
    }

    // 更新DI寄存器（根据DF标志）
    const df = this.registerManager.getFlag('DF');
    // 使用RDI寄存器，不支持EDI（因为Registers枚举中没有定义）
    const diRegister = Registers.RDI;
    let diValue = this.registerManager.getRegister(diRegister);

    if (df) {
      diValue -= BigInt(dataSize);
    } else {
      diValue += BigInt(dataSize);
    }

    this.registerManager.setRegister(diRegister, diValue);
  }

  // VMCALL - 虚拟机调用
  private executeVmcall(): void {
    // VMCALL指令用于从客户机操作系统调用虚拟机监控器
    console.log('VMCALL instruction executed');
    // 实际实现中应该有虚拟机监控器的模拟代码
  }

  // VMLAUNCH - 启动虚拟机
  private executeVmlaunch(): void {
    // VMLAUNCH指令用于启动虚拟机执行
    console.log('VMLAUNCH instruction executed');
  }

  // VMRESUME - 恢复虚拟机执行
  private executeVmresume(): void {
    // VMRESUME指令用于恢复虚拟机执行
    console.log('VMRESUME instruction executed');
  }

  // VMXOFF - 关闭VMX操作
  private executeVmxoff(): void {
    // VMXOFF指令用于禁用VMX操作
    console.log('VMXOFF instruction executed');
  }

  // MONITOR - 设置硬件监控断点
  private executeMonitor(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算监控地址
    const address = this.calculateEffectiveAddress(instruction);
    console.log(`MONITOR instruction executed on address: 0x${address.toString(16)}`);
    // 实际实现中应该设置硬件监控断点
  }

  // MWAIT - 监控等待
  private executeMwait(instruction: DecodedInstruction): void {
    // MWAIT指令用于在监控地址被访问时唤醒处理器
    console.log('MWAIT instruction executed');
    // 实际实现中应该使处理器进入低功耗状态并等待事件
  }

  // =============================================================================
  // 多媒体指令实现 (基础支持)
  // =============================================================================

  // FXSAVE - 保存x87 FPU、MMX、XMM状态
  private executeFxsave(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 在JavaScript中模拟FXSAVE操作
    console.log(`FXSAVE instruction executed, saving FPU state to address: 0x${address.toString(16)}`);

    // 实际实现中应该保存FPU状态到内存
    // 这里我们只是写入一些模拟数据
    for (let i = 0n; i < 512n; i++) {
      this.writeByteToMemory(address + i, 0);
    }
  }

  // FXRSTOR - 恢复x87 FPU、MMX、XMM状态
  private executeFxrstor(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 在JavaScript中模拟FXRSTOR操作
    console.log(`FXRSTOR instruction executed, restoring FPU state from address: 0x${address.toString(16)}`);

    // 实际实现中应该从内存恢复FPU状态
  }

  // MOVZX r64, r/m8 - 零扩展字节到四字
  // MOVZX r64, r/m16 - 零扩展字到四字
  private executeMovzx(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    let srcValue: u64;

    // 根据操作码确定源操作数大小
    if (instruction.opcode === 0x0FB6) {
      // MOVZX r64, r/m8
      if (instruction.modrm.mod === 3) {
        // 源是寄存器，读取低8位
        srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
      } else {
        // 源是内存，读取字节
        const address = this.calculateEffectiveAddress(instruction);
        srcValue = BigInt(this.readByteFromMemory(address));
      }
    } else {
      // MOVZX r64, r/m16
      if (instruction.modrm.mod === 3) {
        // 源是寄存器，读取低16位
        srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
      } else {
        // 源是内存，读取字
        const address = this.calculateEffectiveAddress(instruction);
        srcValue = this.readWordFromMemory(address);
      }
    }

    // 零扩展到64位并写入目标寄存器
    this.registerManager.setRegister(destReg, srcValue);
  }

  // MOVSX r64, r/m8 - 符号扩展字节到四字
  // MOVSX r64, r/m16 - 符号扩展字到四字
  private executeMovsx(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    let srcValue: u64;

    // 根据操作码确定源操作数大小
    if (instruction.opcode === 0x0FBE) {
      // MOVSX r64, r/m8
      if (instruction.modrm.mod === 3) {
        // 源是寄存器，读取低8位
        srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
      } else {
        // 源是内存，读取字节
        const address = this.calculateEffectiveAddress(instruction);
        srcValue = BigInt(this.readByteFromMemory(address));
      }
      // 符号扩展：如果第7位是1，则填充1
      if (srcValue & 0x80n) {
        srcValue |= 0xFFFFFFFFFFFFFF00n;
      }
    } else {
      // MOVSX r64, r/m16
      if (instruction.modrm.mod === 3) {
        // 源是寄存器，读取低16位
        srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
      } else {
        // 源是内存，读取字
        const address = this.calculateEffectiveAddress(instruction);
        srcValue = this.readWordFromMemory(address);
      }
      // 符号扩展：如果第15位是1，则填充1
      if (srcValue & 0x8000n) {
        srcValue |= 0xFFFFFFFFFFFF0000n;
      }
    }

    // 写入目标寄存器
    this.registerManager.setRegister(destReg, srcValue);
  }

  // LDMXCSR - 加载MXCSR寄存器
  private executeLdmxcsr(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 使用readQwordFromMemory并转换为双字值
    const qwordValue = this.readQwordFromMemory(address);
    const mxcsrValue = qwordValue & 0xFFFFFFFFn;

    // 模拟设置MXCSR寄存器
    console.log(`LDMXCSR instruction executed, loaded MXCSR value: 0x${mxcsrValue.toString(16)}`);
  }

  // STMXCSR - 存储MXCSR寄存器
  private executeStmxcsr(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 模拟获取MXCSR值
    const mxcsrValue = BigInt(0x1F80); // 标准默认值

    // 使用writeQwordToMemory并确保只写入双字部分
    this.writeQwordToMemory(address, mxcsrValue);

    console.log(`STMXCSR instruction executed, stored MXCSR value: 0x${mxcsrValue.toString(16)}`);
    console.log(`STMXCSR instruction executed, stored MXCSR value: 0x${mxcsrValue.toString(16)}`);
  }

  // MOVSXD r64, r/m32 - 符号扩展双字到四字
  private executeMovsxd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    const destReg = instruction.modrm.reg;
    let srcValue: u64;

    // 源操作数是32位值
    if (instruction.modrm.mod === 3) {
      // 源是寄存器，读取低32位
      srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
    } else {
      // 源是内存，读取双字
      const address = this.calculateEffectiveAddress(instruction);
      srcValue = this.readDwordFromMemory(address);
    }

    // 符号扩展：如果第31位是1，则填充1
    if (srcValue & 0x80000000n) {
      srcValue |= 0xFFFFFFFF00000000n;
    }

    // 写入目标寄存器
    this.registerManager.setRegister(destReg, srcValue);
  }

  // BSWAP r64 - 字节交换（64位寄存器）
  private executeBswap(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 在64位模式下，BSWAP仅适用于寄存器操作
    if (instruction.modrm.mod !== 3) {
      console.warn('BSWAP instruction: Only register operands are supported in 64-bit mode');
      return;
    }

    const reg = instruction.modrm.rm;
    let value = this.registerManager.getRegister(reg);

    // 执行64位字节交换
    // 将字节顺序从 [B7 B6 B5 B4 B3 B2 B1 B0] 转换为 [B0 B1 B2 B3 B4 B5 B6 B7]
    value =
      ((value & 0x00000000000000FFn) << 56n) | // B0 移到 B7
      ((value & 0x000000000000FF00n) << 40n) | // B1 移到 B6
      ((value & 0x0000000000FF0000n) << 24n) | // B2 移到 B5
      ((value & 0x00000000FF000000n) << 8n) |  // B3 移到 B4
      ((value & 0x000000FF00000000n) >> 8n) |  // B4 移到 B3
      ((value & 0x0000FF0000000000n) >> 24n) | // B5 移到 B2
      ((value & 0x00FF000000000000n) >> 40n) | // B6 移到 B1
      ((value & 0xFF00000000000000n) >> 56n);  // B7 移到 B0

    // 写回结果到寄存器
    this.registerManager.setRegister(reg, value);
  }

  /**
   * 执行RDPMC指令 - 读取性能监控计数器
   * 在64位模式下，RDPMC指令将指定计数器的值加载到EDX:EAX寄存器中
   * ECX寄存器指定要读取的计数器索引
   */
  private executeRdpmc(): void {
    // 获取ECX寄存器的值，用于确定要读取的计数器
    const counterIndex = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;

    // 在这个简单的模拟器中，我们返回0值，因为我们没有实现真正的性能监控计数器
    // 真实实现中应该根据counterIndex返回相应计数器的值
    const counterValue = 0n;

    // 将结果写入EDX:EAX寄存器
    // EDX包含高32位，EAX包含低32位
    this.registerManager.setRegister(Registers.RAX, counterValue & 0xFFFFFFFFn);
    this.registerManager.setRegister(Registers.RDX, counterValue >> 32n);
  }

  /**
   * 执行CLTS指令 - 清除任务切换标志
   * 该指令清除CR0寄存器中的TS标志位
   * 只有在特权级别为0时才能执行
   */
  private executeClts(): void {
    // 检查当前特权级别(CPL)
    const cpl = this.registerManager.getCurrentPrivilegeLevel();
    if (cpl !== 0) {
      // 如果CPL不为0，触发保护故障
      throw new Error('Protection fault: CLTS instruction can only be executed at CPL 0');
    }

    // 获取当前CR0寄存器的值
    let cr0 = this.registerManager.getCr0();

    // 清除TS标志位(第3位)
    cr0 &= ~(1n << 3n);

    // 写回CR0寄存器
    this.registerManager.setCr0(cr0);

    console.log('CLTS instruction executed: TS flag cleared in CR0 register');
  }

  /**
   * 执行CALLF指令 - 远调用指令
   * 该指令执行到指定段和偏移量的远调用
   * 操作码格式: CALL FAR m16:16/m16:32/m16:64
   */
  private executeCallf(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 计算目标地址
    const targetAddress = this.calculateEffectiveAddress(instruction);

    // 读取目标段选择器和偏移量
    // 在64位模式下，这是一个16:64的远指针
    const segmentSelector = this.readWordFromMemory(targetAddress);
    const offset = this.readQwordFromMemory(targetAddress + 2n);

    // 保存当前CS段选择器和IP指令指针到栈
    const currentCs = this.registerManager.getSegmentRegister(SegmentRegisters.CS);
    const currentIp = this.registerManager.getInstructionPointer();

    // 压入CS和EIP/RIP
    this.push(BigInt(currentCs));
    this.push(currentIp);

    // 设置新的CS段选择器和IP指令指针
    this.registerManager.setSegmentRegister(SegmentRegisters.CS, Number(segmentSelector));
    this.registerManager.setInstructionPointer(offset);

    console.log(`CALLF instruction executed: jumped to segment 0x${segmentSelector.toString(16)}, offset 0x${offset.toString(16)}`);
  }

  /**
   * 执行XGETBV指令 - 读取扩展控制寄存器
   * 在64位模式下，XGETBV指令将指定扩展控制寄存器的值加载到EDX:EAX寄存器中
   * ECX寄存器指定要读取的扩展控制寄存器索引（通常为0，表示XCR0）
   */
  private executeXgetbv(): void {
    // 获取ECX寄存器的值，用于确定要读取的扩展控制寄存器
    const regIndex = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;

    // 目前只支持XCR0寄存器（regIndex = 0）
    let xcrValue = 0n;
    if (regIndex === 0n) {
      // 读取XCR0寄存器的值
      xcrValue = this.registerManager.getXcr0();
    } else {
      // 对于不支持的寄存器，返回0值
      console.warn(`XGETBV: Unsupported register index ${regIndex}`);
    }

    // 将结果写入EDX:EAX寄存器
    // EDX包含高32位，EAX包含低32位
    this.registerManager.setRegister(Registers.RAX, xcrValue & 0xFFFFFFFFn);
    this.registerManager.setRegister(Registers.RDX, xcrValue >> 32n);
  }

  /**
   * 执行XSETBV指令 - 设置扩展控制寄存器
   * 在64位模式下，XSETBV指令将EDX:EAX寄存器的值存储到指定的扩展控制寄存器中
   * ECX寄存器指定要设置的扩展控制寄存器索引（通常为0，表示XCR0）
   */
  private executeXsetbv(): void {
    // 获取ECX寄存器的值，用于确定要设置的扩展控制寄存器
    const regIndex = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;

    // 目前只支持XCR0寄存器（regIndex = 0）
    if (regIndex === 0n) {
      // 从EDX:EAX寄存器中读取值
      const eaxValue = this.registerManager.getRegister(Registers.RAX);
      const edxValue = this.registerManager.getRegister(Registers.RDX);
      const xcrValue = (edxValue << 32n) | (eaxValue & 0xFFFFFFFFn);

      // 设置XCR0寄存器的值
      // 注意：在真实实现中，应该检查XCR0的每一位是否可以被设置，根据处理器的支持情况
      this.registerManager.setXcr0(xcrValue);
    } else {
      console.warn(`XSETBV: Unsupported register index ${regIndex}`);
    }
  }

  /**
   * 执行ADDPD/ADDPS指令 - 双精度/单精度浮点数加法
   * ADDPD: 将两个128位压缩双精度浮点数相加（两个64位浮点数）
   * ADDPS: 将两个128位压缩单精度浮点数相加（四个32位浮点数）
   * 根据指令前缀确定是单精度还是双精度
   */
  private executeAddpdAddps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 检查是否有66H前缀来区分ADDPS和ADDPD
    // 没有66H前缀是ADDPS（单精度），有66H前缀是ADDPD（双精度）
    const isDoublePrecision = instruction.prefixes.has(PrefixType.OPERAND_SIZE);

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    if (isDoublePrecision) {
      // ADDPD - 双精度浮点数加法（两个64位浮点数）
      for (let i = 0; i < 2; i++) {
        // 读取双精度浮点数
        const destFloat = this.readDoubleFromUint8Array(destData, i * 8);
        const srcFloat = this.readDoubleFromUint8Array(srcData, i * 8);

        // 执行加法
        const resultFloat = destFloat + srcFloat;

        // 写入结果
        this.writeDoubleToUint8Array(resultData, i * 8, resultFloat);
      }
    } else {
      // ADDPS - 单精度浮点数加法（四个32位浮点数）
      for (let i = 0; i < 4; i++) {
        // 读取单精度浮点数
        const destFloat = this.readFloatFromUint8Array(destData, i * 4);
        const srcFloat = this.readFloatFromUint8Array(srcData, i * 4);

        // 执行加法
        const resultFloat = destFloat + srcFloat;

        // 写入结果
        this.writeFloatToUint8Array(resultData, i * 4, resultFloat);
      }
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  /**
   * 执行CMPPS/CMPPD指令 - 单精度/双精度浮点数比较
   * CMPPS: 比较两个128位压缩单精度浮点数（四个32位浮点数）
   * CMPPD: 比较两个128位压缩双精度浮点数（两个64位浮点数）
   * 根据指令前缀确定是单精度还是双精度
   * 比较结果为全1或全0的掩码
   */
  private executeCmppsCmppd(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 检查是否有66H前缀来区分CMPPS和CMPPD
    // 没有66H前缀是CMPPS（单精度），有66H前缀是CMPPD（双精度）
    const isDoublePrecision = instruction.prefixes.has(PrefixType.OPERAND_SIZE);

    // 获取比较类型（从立即数）
    const compareType = Number(instruction.immediate & 0x7n);

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    if (isDoublePrecision) {
      // CMPPD - 双精度浮点数比较（两个64位浮点数）
      for (let i = 0; i < 2; i++) {
        // 读取双精度浮点数
        const destFloat = this.readDoubleFromUint8Array(destData, i * 8);
        const srcFloat = this.readDoubleFromUint8Array(srcData, i * 8);

        // 执行比较并获取结果掩码
        const resultMask = this.compareFloatAndGetMask(destFloat, srcFloat, compareType, 8);

        // 写入结果掩码
        for (let j = 0; j < 8; j++) {
          resultData[i * 8 + j] = resultMask[j];
        }
      }
    } else {
      // CMPPS - 单精度浮点数比较（四个32位浮点数）
      for (let i = 0; i < 4; i++) {
        // 读取单精度浮点数
        const destFloat = this.readFloatFromUint8Array(destData, i * 4);
        const srcFloat = this.readFloatFromUint8Array(srcData, i * 4);

        // 执行比较并获取结果掩码
        const resultMask = this.compareFloatAndGetMask(destFloat, srcFloat, compareType, 4);

        // 写入结果掩码
        for (let j = 0; j < 4; j++) {
          resultData[i * 4 + j] = resultMask[j];
        }
      }
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  /**
   * 比较两个浮点数并返回结果掩码
   * @param dest 目标浮点数
   * @param src 源浮点数
   * @param compareType 比较类型（0-7）
   * @param size 浮点数大小（4表示单精度，8表示双精度）
   * @returns 结果掩码（全1或全0）
   */
  private compareFloatAndGetMask(dest: number, src: number, compareType: number, size: number): Uint8Array {
    // 创建结果掩码数组
    const mask = new Uint8Array(size);

    // 根据比较类型执行比较
    let result = false;

    switch (compareType) {
      case 0: // 相等 (EQ)
        result = dest === src;
        break;
      case 1: // 小于 (LT)
        result = dest < src;
        break;
      case 2: // 小于等于 (LE)
        result = dest <= src;
        break;
      case 3: // 无序 (UNORD)
        result = isNaN(dest) || isNaN(src);
        break;
      case 4: // 不相等 (NEQ)
        result = dest !== src;
        break;
      case 5: // 不小于 (NLT)
        result = dest >= src;
        break;
      case 6: // 不小于等于 (NLE)
        result = dest > src;
        break;
      case 7: // 有序 (ORD)
        result = !isNaN(dest) && !isNaN(src);
        break;
    }

    // 如果比较结果为真，设置全1掩码；否则设置全0掩码
    const fillValue = result ? 0xFF : 0x00;
    for (let i = 0; i < size; i++) {
      mask[i] = fillValue;
    }

    return mask;
  }

  /**
   * 执行ANDPS指令 - 单精度浮点数按位与
   * 将两个128位压缩单精度浮点数按位与（四个32位浮点数，按位操作）
   */
  private executeAndps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      // 源操作数是内存
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 按位与操作
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      resultData[i] = destData[i] & srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // MAXPS - 单精度浮点数最大值
  private executeMaxps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 执行单精度浮点数最大值（四个32位浮点数）
    for (let i = 0; i < 4; i++) {
      // 读取单精度浮点数
      const destFloat = this.readFloatFromUint8Array(destData, i * 4);
      const srcFloat = this.readFloatFromUint8Array(srcData, i * 4);

      // 执行最大值操作
      const resultFloat = Math.max(destFloat, srcFloat);

      // 写入结果
      this.writeFloatToUint8Array(resultData, i * 4, resultFloat);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // MINPS - 单精度浮点数最小值
  private executeMinps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 执行单精度浮点数最小值（四个32位浮点数）
    for (let i = 0; i < 4; i++) {
      // 读取单精度浮点数
      const destFloat = this.readFloatFromUint8Array(destData, i * 4);
      const srcFloat = this.readFloatFromUint8Array(srcData, i * 4);

      // 执行最小值操作
      const resultFloat = Math.min(destFloat, srcFloat);

      // 写入结果
      this.writeFloatToUint8Array(resultData, i * 4, resultFloat);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // MOVSD - 双精度浮点数移动
  private executeMovsd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      const srcData = this.registerManager.getXmmRegister(srcXmmReg);
      const destData = new Uint8Array(XMM_REGISTER_SIZE);

      // 复制低64位（双精度浮点数）
      for (let i = 0; i < 8; i++) {
        destData[i] = srcData[i];
      }

      // 写回结果到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, destData);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      const srcData = this.memoryManager.read(address, 8); // 读取64位
      const destData = new Uint8Array(XMM_REGISTER_SIZE);

      // 复制到目标XMM寄存器的低64位
      for (let i = 0; i < 8; i++) {
        destData[i] = srcData[i];
      }

      // 写回结果到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, destData);
    }
  }

  // MOVSS - 单精度浮点数移动
  private executeMovss(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      const srcData = this.registerManager.getXmmRegister(srcXmmReg);
      const destData = this.registerManager.getXmmRegister(destXmmReg);

      // 复制低32位（单精度浮点数）
      for (let i = 0; i < 4; i++) {
        destData[i] = srcData[i];
      }

      // 写回结果到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, destData);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      const srcData = this.memoryManager.read(address, 4); // 读取32位
      const destData = this.registerManager.getXmmRegister(destXmmReg);

      // 复制到目标XMM寄存器的低32位
      for (let i = 0; i < 4; i++) {
        destData[i] = srcData[i];
      }

      // 写回结果到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, destData);
    }
  }

  // MOVSW - 字串移动指令
  private executeMovsw(instruction: DecodedInstruction): void {
    // 获取源地址 (RSI)
    const sourceAddress = this.registerManager.getRegister(Registers.RSI);
    // 获取目标地址 (RDI)
    const destinationAddress = this.registerManager.getRegister(Registers.RDI);

    // 读取源字（16位）
    const sourceValue = this.readWordFromMemory(sourceAddress);
    // 写入目标地址
    this.writeWordToMemory(destinationAddress, Number(sourceValue));

    // 根据方向标志更新索引寄存器
    const df = this.registerManager.getFlag('DF');
    if (df) {
      // DF=1，递减
      this.registerManager.setRegister(Registers.RSI, sourceAddress - 2n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress - 2n);
    } else {
      // DF=0，递增
      this.registerManager.setRegister(Registers.RSI, sourceAddress + 2n);
      this.registerManager.setRegister(Registers.RDI, destinationAddress + 2n);
    }
  }

  // MOVUPS - 非对齐单精度浮点数移动
  private executeMovups(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      const srcData = this.registerManager.getXmmRegister(srcXmmReg);

      // 复制数据到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, srcData);
    } else {
      // 源操作数是内存（非对齐）
      const address = this.calculateEffectiveAddress(instruction);
      const srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);

      // 复制数据到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, srcData);
    }
  }

  // LDDQU - 非对齐双字加载
  private executeLddqu(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;

    // 源操作数是内存（非对齐）
    const address = this.calculateEffectiveAddress(instruction);
    const srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);

    // 复制数据到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, srcData);
  }

  // PSLLWV - 可变宽度左移位（字）
  private executePsllwv(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数（要移位的字）是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 获取移位计数字段（假设在另一个寄存器中）
    // 在实际指令中，这通常来自另一个XMM寄存器的低8位
    const countXmmReg = instruction.modrm.reg;
    const countData = this.registerManager.getXmmRegister(countXmmReg);
    const countView = new DataView(countData.buffer, countData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个字执行不同宽度的左移位
    // XMM寄存器包含8个字（16字节）
    for (let i = 0; i < 8; i++) {
      // 读取源字
      const srcWord = srcView.getInt16(i * 2, true);
      // 读取移位计数（取低6位，因为移位计数只需要低6位）
      const count = (countView.getUint8(i) & 0x3F);
      
      // 执行左移位操作
      let resultWord = srcWord << count;
      
      // 确保结果保持在16位有符号整数范围内
      resultWord = resultWord & 0xFFFF;
      
      // 写回结果
      resultView.setInt16(i * 2, resultWord, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSLLDV - 可变宽度左移位（双字）
  private executePslldv(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数（要移位的双字）是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 获取移位计数字段（假设在另一个寄存器中）
    // 在实际指令中，这通常来自另一个XMM寄存器的低8位
    const countXmmReg = instruction.modrm.reg;
    const countData = this.registerManager.getXmmRegister(countXmmReg);
    const countView = new DataView(countData.buffer, countData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个双字执行不同宽度的左移位
    // XMM寄存器包含4个双字（16字节）
    for (let i = 0; i < 4; i++) {
      // 读取源双字
      const srcDword = srcView.getInt32(i * 4, true);
      // 读取移位计数（取低6位，因为移位计数只需要低6位）
      const count = (countView.getUint8(i) & 0x3F);
      
      // 执行左移位操作
      let resultDword = srcDword << count;
      
      // 确保结果保持在32位有符号整数范围内
      resultDword = resultDword & 0xFFFFFFFF;
      
      // 写回结果
      resultView.setInt32(i * 4, resultDword, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSLLQV - 可变宽度左移位（四字）
  private executePsllqv(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数（要移位的四字）是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 获取移位计数字段（假设在另一个寄存器中）
    // 在实际指令中，这通常来自另一个XMM寄存器的低8位
    const countXmmReg = instruction.modrm.reg;
    const countData = this.registerManager.getXmmRegister(countXmmReg);
    const countView = new DataView(countData.buffer, countData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个四字执行不同宽度的左移位
    // XMM寄存器包含2个四字（16字节）
    for (let i = 0; i < 2; i++) {
      // 读取源四字
      const srcQword = srcView.getBigUint64(i * 8, true);
      // 读取移位计数（取低7位，因为移位计数只需要低7位）
      const count = (countView.getUint8(i) & 0x7F);
      
      // 执行左移位操作
      let resultQword = count < 64 ? srcQword << BigInt(count) : 0n;
      
      // 写回结果
      resultView.setBigUint64(i * 8, resultQword, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PMAXSW - 带符号字最大值
  private executePmaxsw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 从目标寄存器获取原始数据
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个字执行最大值操作
    // XMM寄存器包含8个字（16字节）
    for (let i = 0; i < 8; i++) {
      // 读取源字和目标字
      const srcWord = srcView.getInt16(i * 2, true);
      const destWord = destView.getInt16(i * 2, true);
      
      // 计算最大值
      const maxWord = Math.max(srcWord, destWord);
      
      // 写回结果
      resultView.setInt16(i * 2, maxWord, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PMINSW - 带符号字最小值
  private executePminsw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 从目标寄存器获取原始数据
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个字执行最小值操作
    // XMM寄存器包含8个字（16字节）
    for (let i = 0; i < 8; i++) {
      // 读取源字和目标字
      const srcWord = srcView.getInt16(i * 2, true);
      const destWord = destView.getInt16(i * 2, true);
      
      // 计算最小值
      const minWord = Math.min(srcWord, destWord);
      
      // 写回结果
      resultView.setInt16(i * 2, minWord, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSUBB - 字节减法
  private executePsubb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 对每个字节执行减法操作
    // XMM寄存器包含16个字节
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      // 执行无符号字节减法
      resultData[i] = destData[i] - srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSUBW - 字减法
  private executePsubw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个字执行减法操作
    // XMM寄存器包含8个字（16字节）
    for (let i = 0; i < 8; i++) {
      // 读取源字和目标字
      const srcWord = srcView.getInt16(i * 2, true);
      const destWord = destView.getInt16(i * 2, true);
      
      // 执行有符号字减法
      const subResult = destWord - srcWord;
      
      // 写回结果
      resultView.setInt16(i * 2, subResult, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSUBD - 双字减法
  private executePsubd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个双字执行减法操作
    // XMM寄存器包含4个双字（16字节）
    for (let i = 0; i < 4; i++) {
      // 读取源双字和目标双字
      const srcDword = srcView.getInt32(i * 4, true);
      const destDword = destView.getInt32(i * 4, true);
      
      // 执行有符号双字减法
      const subResult = destDword - srcDword;
      
      // 写回结果
      resultView.setInt32(i * 4, subResult, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSUBQ - 四字减法
  private executePsubq(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个四字执行减法操作
    // XMM寄存器包含2个四字（16字节）
    for (let i = 0; i < 2; i++) {
      // 读取源四字和目标四字
      const srcQword = srcView.getBigInt64(i * 8, true);
      const destQword = destView.getBigInt64(i * 8, true);
      
      // 执行有符号四字减法
      const subResult = destQword - srcQword;
      
      // 写回结果
      resultView.setBigInt64(i * 8, subResult, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PADDB - 字节加法
  private executePaddb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 对每个字节执行加法操作
    // XMM寄存器包含16个字节
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      // 执行无符号字节加法
      resultData[i] = destData[i] + srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PADDW - 字加法
  private executePaddw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个字执行加法操作
    // XMM寄存器包含8个字（16字节）
    for (let i = 0; i < 8; i++) {
      // 读取源字和目标字
      const srcWord = srcView.getInt16(i * 2, true);
      const destWord = destView.getInt16(i * 2, true);
      
      // 执行有符号字加法
      const addResult = destWord + srcWord;
      
      // 写回结果
      resultView.setInt16(i * 2, addResult, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PADDD - 双字加法
  private executePaddd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个双字执行加法操作
    // XMM寄存器包含4个双字（16字节）
    for (let i = 0; i < 4; i++) {
      // 读取源双字和目标双字
      const srcDword = srcView.getInt32(i * 4, true);
      const destDword = destView.getInt32(i * 4, true);
      
      // 执行有符号双字加法
      const addResult = destDword + srcDword;
      
      // 写回结果
      resultView.setInt32(i * 4, addResult, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSHUFB - 字节洗牌
  private executePshufb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 对每个字节执行洗牌操作
    // XMM寄存器包含16个字节
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      // 如果srcData[i]的最高位（位7）为1，则resultData[i]设置为0
      // 否则，resultData[i]设置为destData[srcData[i] & 0x0F]（只取低4位作为索引）
      if (srcData[i] & 0x80) {
        resultData[i] = 0;
      } else {
        const index = srcData[i] & 0x0F;
        resultData[i] = destData[index];
      }
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PHADDW - 水平字加法
  private executePhaddw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 水平字加法操作
    // XMM寄存器包含8个字（16字节）
    // 对目标寄存器中的相邻字对执行加法，然后对源寄存器中的相邻字对执行加法
    // 结果按照 dest[0]+dest[1], dest[2]+dest[3], src[0]+src[1], src[2]+src[3], dest[4]+dest[5], dest[6]+dest[7], src[4]+src[5], src[6]+src[7] 的顺序排列
    
    // 处理目标寄存器中的前4个字（组成2对）
    resultView.setInt16(0 * 2, destView.getInt16(0 * 2, true) + destView.getInt16(1 * 2, true), true);
    resultView.setInt16(1 * 2, destView.getInt16(2 * 2, true) + destView.getInt16(3 * 2, true), true);
    
    // 处理源寄存器中的前4个字（组成2对）
    resultView.setInt16(2 * 2, srcView.getInt16(0 * 2, true) + srcView.getInt16(1 * 2, true), true);
    resultView.setInt16(3 * 2, srcView.getInt16(2 * 2, true) + srcView.getInt16(3 * 2, true), true);
    
    // 处理目标寄存器中的后4个字（组成2对）
    resultView.setInt16(4 * 2, destView.getInt16(4 * 2, true) + destView.getInt16(5 * 2, true), true);
    resultView.setInt16(5 * 2, destView.getInt16(6 * 2, true) + destView.getInt16(7 * 2, true), true);
    
    // 处理源寄存器中的后4个字（组成2对）
    resultView.setInt16(6 * 2, srcView.getInt16(4 * 2, true) + srcView.getInt16(5 * 2, true), true);
    resultView.setInt16(7 * 2, srcView.getInt16(6 * 2, true) + srcView.getInt16(7 * 2, true), true);

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PHADDD - 水平双字加法
  private executePhaddd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 水平双字加法操作
    // XMM寄存器包含4个双字（16字节）
    // 对目标寄存器中的相邻双字对执行加法，然后对源寄存器中的相邻双字对执行加法
    // 结果按照 dest[0]+dest[1], dest[2]+dest[3], src[0]+src[1], src[2]+src[3] 的顺序排列
    
    // 处理目标寄存器中的前4个双字（组成2对）
    resultView.setInt32(0 * 4, destView.getInt32(0 * 4, true) + destView.getInt32(1 * 4, true), true);
    resultView.setInt32(1 * 4, destView.getInt32(2 * 4, true) + destView.getInt32(3 * 4, true), true);
    
    // 处理源寄存器中的前4个双字（组成2对）
    resultView.setInt32(2 * 4, srcView.getInt32(0 * 4, true) + srcView.getInt32(1 * 4, true), true);
    resultView.setInt32(3 * 4, srcView.getInt32(2 * 4, true) + srcView.getInt32(3 * 4, true), true);

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PHADDSW - 水平带符号饱和字加法
  private executePhaddsw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 16位有符号整数的饱和范围
    const INT16_MIN = -32768;
    const INT16_MAX = 32767;
    
    // 水平带符号饱和字加法操作
    // XMM寄存器包含8个字（16字节）
    // 对目标寄存器中的相邻字对执行加法，然后对源寄存器中的相邻字对执行加法
    // 结果按照 dest[0]+dest[1], dest[2]+dest[3], src[0]+src[1], src[2]+src[3], dest[4]+dest[5], dest[6]+dest[7], src[4]+src[5], src[6]+src[7] 的顺序排列
    
    // 处理目标寄存器中的前4个字（组成2对）
    for (let i = 0; i < 2; i++) {
      const value1 = destView.getInt16(i * 4, true);
      const value2 = destView.getInt16(i * 4 + 2, true);
      const sum = value1 + value2;
      // 应用饱和处理
      const saturatedSum = Math.max(INT16_MIN, Math.min(INT16_MAX, sum));
      resultView.setInt16(i * 2, saturatedSum, true);
    }
    
    // 处理源寄存器中的前4个字（组成2对）
    for (let i = 0; i < 2; i++) {
      const value1 = srcView.getInt16(i * 4, true);
      const value2 = srcView.getInt16(i * 4 + 2, true);
      const sum = value1 + value2;
      // 应用饱和处理
      const saturatedSum = Math.max(INT16_MIN, Math.min(INT16_MAX, sum));
      resultView.setInt16((2 + i) * 2, saturatedSum, true);
    }
    
    // 处理目标寄存器中的后4个字（组成2对）
    for (let i = 0; i < 2; i++) {
      const value1 = destView.getInt16(8 + i * 4, true);
      const value2 = destView.getInt16(8 + i * 4 + 2, true);
      const sum = value1 + value2;
      // 应用饱和处理
      const saturatedSum = Math.max(INT16_MIN, Math.min(INT16_MAX, sum));
      resultView.setInt16((4 + i) * 2, saturatedSum, true);
    }
    
    // 处理源寄存器中的后4个字（组成2对）
    for (let i = 0; i < 2; i++) {
      const value1 = srcView.getInt16(8 + i * 4, true);
      const value2 = srcView.getInt16(8 + i * 4 + 2, true);
      const sum = value1 + value2;
      // 应用饱和处理
      const saturatedSum = Math.max(INT16_MIN, Math.min(INT16_MAX, sum));
      resultView.setInt16((6 + i) * 2, saturatedSum, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PMADDUBSW - 无符号字节乘法带符号字加法
  private executePmaddubsw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 16位有符号整数的饱和范围
    const INT16_MIN = -32768;
    const INT16_MAX = 32767;
    
    // 无符号字节乘法带符号字加法操作
    // XMM寄存器包含16个字节
    // 对目标和源寄存器中的每对相邻字节执行操作：
    // 1. 目标字节被当作无符号字节
    // 2. 源字节被当作有符号字节
    // 3. 将它们相乘
    // 4. 将相邻的两个乘积相加
    // 5. 对结果进行16位有符号整数的饱和处理
    
    // 处理16字节数据，每两个字节为一组
    for (let i = 0; i < 8; i++) {
      // 获取目标寄存器中的无符号字节
      const destByte1 = destData[i * 2];
      const destByte2 = destData[i * 2 + 1];
      
      // 获取源寄存器中的有符号字节
      // 使用DataView读取int8值
      const srcByte1 = (srcData[i * 2] << 24) >> 24; // 将无符号字节转换为有符号字节
      const srcByte2 = (srcData[i * 2 + 1] << 24) >> 24; // 将无符号字节转换为有符号字节
      
      // 计算乘积并相加
      const product1 = destByte1 * srcByte1;
      const product2 = destByte2 * srcByte2;
      const sum = product1 + product2;
      
      // 应用饱和处理
      const saturatedSum = Math.max(INT16_MIN, Math.min(INT16_MAX, sum));
      
      // 存储结果到结果数组
      resultView.setInt16(i * 2, saturatedSum, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PHSUBW - 水平字减法
  private executePhsubw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 水平字减法操作
    // XMM寄存器包含8个字（16字节）
    // 对目标寄存器中的相邻字对执行减法，然后对源寄存器中的相邻字对执行减法
    // 结果按照 dest[0]-dest[1], dest[2]-dest[3], src[0]-src[1], src[2]-src[3], dest[4]-dest[5], dest[6]-dest[7], src[4]-src[5], src[6]-src[7] 的顺序排列
    
    // 处理目标寄存器中的前4个字（组成2对）
    resultView.setInt16(0 * 2, destView.getInt16(0 * 2, true) - destView.getInt16(1 * 2, true), true);
    resultView.setInt16(1 * 2, destView.getInt16(2 * 2, true) - destView.getInt16(3 * 2, true), true);
    
    // 处理源寄存器中的前4个字（组成2对）
    resultView.setInt16(2 * 2, srcView.getInt16(0 * 2, true) - srcView.getInt16(1 * 2, true), true);
    resultView.setInt16(3 * 2, srcView.getInt16(2 * 2, true) - srcView.getInt16(3 * 2, true), true);
    
    // 处理目标寄存器中的后4个字（组成2对）
    resultView.setInt16(4 * 2, destView.getInt16(4 * 2, true) - destView.getInt16(5 * 2, true), true);
    resultView.setInt16(5 * 2, destView.getInt16(6 * 2, true) - destView.getInt16(7 * 2, true), true);
    
    // 处理源寄存器中的后4个字（组成2对）
    resultView.setInt16(6 * 2, srcView.getInt16(4 * 2, true) - srcView.getInt16(5 * 2, true), true);
    resultView.setInt16(7 * 2, srcView.getInt16(6 * 2, true) - srcView.getInt16(7 * 2, true), true);

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PHSUBD - 水平双字减法
  private executePhsubd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 水平双字减法操作
    // XMM寄存器包含4个双字（16字节）
    // 对目标寄存器中的相邻双字对执行减法，然后对源寄存器中的相邻双字对执行减法
    // 结果按照 dest[0]-dest[1], dest[2]-dest[3], src[0]-src[1], src[2]-src[3] 的顺序排列
    
    // 处理目标寄存器中的前4个双字（组成2对）
    resultView.setInt32(0 * 4, destView.getInt32(0 * 4, true) - destView.getInt32(1 * 4, true), true);
    resultView.setInt32(1 * 4, destView.getInt32(2 * 4, true) - destView.getInt32(3 * 4, true), true);
    
    // 处理源寄存器中的前4个双字（组成2对）
    resultView.setInt32(2 * 4, srcView.getInt32(0 * 4, true) - srcView.getInt32(1 * 4, true), true);
    resultView.setInt32(3 * 4, srcView.getInt32(2 * 4, true) - srcView.getInt32(3 * 4, true), true);

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PHSUBSW - 水平带符号饱和字减法
  private executePhsubsw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是另一个XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    
    // 获取源和目标XMM寄存器的值
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 水平带符号饱和字减法操作
    // XMM寄存器包含8个字（16字节）
    // 对目标寄存器中的相邻字对执行减法，然后对源寄存器中的相邻字对执行减法
    // 结果按照 dest[0]-dest[1], dest[2]-dest[3], src[0]-src[1], src[2]-src[3], dest[4]-dest[5], dest[6]-dest[7], src[4]-src[5], src[6]-src[7] 的顺序排列
    // 结果需要进行16位有符号饱和处理（限制在-32768到32767之间）
    
    // 处理目标寄存器中的前4个字（组成2对）
    let result = destView.getInt16(0 * 2, true) - destView.getInt16(1 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(0 * 2, result, true);
    
    result = destView.getInt16(2 * 2, true) - destView.getInt16(3 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(1 * 2, result, true);
    
    // 处理源寄存器中的前4个字（组成2对）
    result = srcView.getInt16(0 * 2, true) - srcView.getInt16(1 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(2 * 2, result, true);
    
    result = srcView.getInt16(2 * 2, true) - srcView.getInt16(3 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(3 * 2, result, true);
    
    // 处理目标寄存器中的后4个字（组成2对）
    result = destView.getInt16(4 * 2, true) - destView.getInt16(5 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(4 * 2, result, true);
    
    result = destView.getInt16(6 * 2, true) - destView.getInt16(7 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(5 * 2, result, true);
    
    // 处理源寄存器中的后4个字（组成2对）
    result = srcView.getInt16(4 * 2, true) - srcView.getInt16(5 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(6 * 2, result, true);
    
    result = srcView.getInt16(6 * 2, true) - srcView.getInt16(7 * 2, true);
    result = Math.max(-32768, Math.min(32767, result)); // 16位有符号饱和处理
    resultView.setInt16(7 * 2, result, true);

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PADDSB - 带符号饱和字节加法
  private executePaddsb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 从目标寄存器获取原始数据
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个字节执行带符号饱和加法操作
    // XMM寄存器包含16个字节
    for (let i = 0; i < 16; i++) {
      // 读取源字节和目标字节
      const srcByte = srcView.getInt8(i);
      const destByte = destView.getInt8(i);
      
      // 执行加法操作
      let sum = srcByte + destByte;
      
      // 饱和处理，确保结果在-128到127范围内
      sum = Math.max(-128, Math.min(127, sum));
      
      // 写回结果
      resultView.setInt8(i, sum);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PADDSW - 带符号饱和字加法
  private executePaddsw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 从目标寄存器获取原始数据
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个字执行带符号饱和加法操作
    // XMM寄存器包含8个字（16字节）
    for (let i = 0; i < 8; i++) {
      // 读取源字和目标字
      const srcWord = srcView.getInt16(i * 2, true);
      const destWord = destView.getInt16(i * 2, true);
      
      // 执行加法操作
      let sum = srcWord + destWord;
      
      // 饱和处理，确保结果在-32768到32767范围内
      sum = Math.max(-32768, Math.min(32767, sum));
      
      // 写回结果
      resultView.setInt16(i * 2, sum, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PMULUDQ - 无符号双字乘法
  private executePmuludq(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 从目标寄存器获取原始数据
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每个双字对执行无符号乘法操作
    // XMM寄存器包含4个双字，但PMULUDQ只处理前两个，结果存储为两个四字
    for (let i = 0; i < 2; i++) {
      // 读取源双字和目标双字（无符号）
      const srcDword = srcView.getUint32(i * 4, true);
      const destDword = destView.getUint32(i * 4, true);
      
      // 执行无符号乘法操作
      const product = BigInt(srcDword) * BigInt(destDword);
      
      // 写回结果（64位）
      resultView.setBigUint64(i * 8, product, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PMADDWD - 字乘法加法
  private executePmaddwd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 从目标寄存器获取原始数据
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 对每对相邻的字执行乘法加法操作
    // XMM寄存器包含8个字（16字节），PMADDWD将其转换为4个双字
    for (let i = 0; i < 4; i++) {
      // 读取源字和目标字
      // 对于每对双字，我们需要两个相邻的字
      const destWord1 = destView.getInt16(i * 4, true);
      const destWord2 = destView.getInt16(i * 4 + 2, true);
      const srcWord1 = srcView.getInt16(i * 4, true);
      const srcWord2 = srcView.getInt16(i * 4 + 2, true);
      
      // 执行乘法加法操作: (w1*d1) + (w2*d2)
      const product1 = destWord1 * srcWord1;
      const product2 = destWord2 * srcWord2;
      const sum = product1 + product2;
      
      // 写回结果（32位双字）
      resultView.setInt32(i * 4, sum, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PSADBW - 绝对差求和
  private executePsadbw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;

    // 从源寄存器获取数据
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 从目标寄存器获取原始数据
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 创建结果数组
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const resultView = new DataView(resultData.buffer, resultData.byteOffset);

    // 计算绝对差求和
    // XMM寄存器包含16个字节，分成两个8字节的块
    for (let block = 0; block < 2; block++) {
      // 计算每个块的前4字节和后4字节的绝对差和
      let sum1 = 0;
      let sum2 = 0;
      
      // 前4字节
      for (let i = 0; i < 4; i++) {
        const destByte = destView.getUint8(block * 8 + i);
        const srcByte = srcView.getUint8(block * 8 + i);
        sum1 += Math.abs(destByte - srcByte);
      }
      
      // 后4字节
      for (let i = 4; i < 8; i++) {
        const destByte = destView.getUint8(block * 8 + i);
        const srcByte = srcView.getUint8(block * 8 + i);
        sum2 += Math.abs(destByte - srcByte);
      }
      
      // 写回结果到对应的双字位置
      resultView.setUint32(block * 8, sum1, true);
      resultView.setUint32(block * 8 + 4, sum2, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // MASKMOVQ/MASKMOVDQU - 掩码移动
  private executeMaskMovDiXmmRegXmmRm(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是内存
    // 直接使用modrm.rm作为地址偏移（简化实现）
    const address = BigInt(instruction.modrm.rm);
    
    // 获取源XMM寄存器（包含要移动的数据）
    const srcXmmReg = instruction.modrm.reg;
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    
    // 获取掩码XMM寄存器（包含掩码）
    const maskXmmReg = instruction.modrm.rm;
    const maskData = this.registerManager.getXmmRegister(maskXmmReg);
    const maskView = new DataView(maskData.buffer, maskData.byteOffset);

    // 确定操作数大小，使用16字节(OWORD)
    const size = 16;
    
    // 创建结果数组
    const resultData = new Uint8Array(size);
    
    // 对每个字节应用掩码
    for (let i = 0; i < size; i++) {
      // 读取掩码字节的最高位
      const maskBit = (maskView.getUint8(i) & 0x80) !== 0;
      
      // 如果掩码位为1，则使用源数据，否则保持内存中的原始数据（简化为使用0）
      resultData[i] = maskBit ? srcView.getUint8(i) : 0;
    }
    
    // 将结果写回内存
    this.memoryManager.write(address, resultData);
  }

  // MOVW - 字移动
  private executeMovw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数
    if (instruction.modrm.mod === 3) {
      // 目标是寄存器
      const destReg = instruction.modrm.reg;
      let srcValue: u64;

      if (instruction.opcode === 0xB8 + destReg) {
        // MOV r16, imm16（在64位模式下通过操作数大小前缀区分）
        srcValue = instruction.immediate! & 0xFFFFn;
      } else {
        // 寄存器到寄存器 或 内存到寄存器
        if (instruction.opcode === 0x89) {
          // MOV r/m16, r16
          srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFn;
        } else {
          // MOV r16, r/m16
          const address = this.calculateEffectiveAddress(instruction);
          srcValue = BigInt(this.readWordFromMemory(address));
        }
      }

      // 写入目标寄存器的低16位
      const destValue = this.registerManager.getRegister(destReg);
      const newValue = (destValue & 0xFFFFFFFFFFFF0000n) | srcValue;
      this.registerManager.setRegister(destReg, newValue);
    } else {
      // 目标是内存
      const address = this.calculateEffectiveAddress(instruction);
      let value: number;

      if (instruction.opcode === 0xC7 && instruction.modrm.reg === 0) {
        // MOV r/m16, imm16
        value = Number(instruction.immediate! & 0xFFFFn);
      } else {
        // 寄存器到内存
        value = Number(this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFn);
      }

      // 写入内存
      this.writeWordToMemory(address, value);
    }
  }

  // MULPD - 双精度浮点数乘法
  private executeMulpd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 执行双精度浮点数乘法（两个64位浮点数）
    for (let i = 0; i < 2; i++) {
      // 读取双精度浮点数
      const destFloat = this.readDoubleFromUint8Array(destData, i * 8);
      const srcFloat = this.readDoubleFromUint8Array(srcData, i * 8);

      // 执行乘法操作
      const resultFloat = destFloat * srcFloat;

      // 写入结果
      this.writeDoubleToUint8Array(resultData, i * 8, resultFloat);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // MULPS - 单精度浮点数乘法
  private executeMulps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 执行单精度浮点数乘法（四个32位浮点数）
    for (let i = 0; i < 4; i++) {
      // 读取单精度浮点数
      const destFloat = this.readFloatFromUint8Array(destData, i * 4);
      const srcFloat = this.readFloatFromUint8Array(srcData, i * 4);

      // 执行乘法操作
      const resultFloat = destFloat * srcFloat;

      // 写入结果
      this.writeFloatToUint8Array(resultData, i * 4, resultFloat);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // ORPS - 单精度浮点数按位或
  private executeOrps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 按位或操作
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      resultData[i] = destData[i] | srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // XORPSD - 双精度浮点数按位异或
  private executeXorpd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 按位异或操作
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      resultData[i] = destData[i] ^ srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // XORPS - 单精度浮点数按位异或
  private executeXorps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 按位异或操作
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      resultData[i] = destData[i] ^ srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // MOVAPD - 对齐双精度浮点数移动
  private executeMovapd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      const srcData = this.registerManager.getXmmRegister(srcXmmReg);

      // 复制数据到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, srcData);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      const srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);

      // 复制数据到目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, srcData);
    }
  }

  // MOVMSKPS - 单精度浮点数掩码移动
  private executeMovmskps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是通用寄存器
    const destReg = instruction.modrm.reg;

    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 计算掩码（提取每个单精度浮点数的符号位）
    let mask = 0n;
    for (let i = 0; i < 4; i++) {
      // 读取符号位（最高位）
      const signBit = (srcData[i * 4 + 3] & 0x80) >> 7;
      // 设置掩码的对应位
      mask |= BigInt(signBit) << BigInt(3 - i);
    }

    // 写入目标寄存器
    this.registerManager.setRegister(destReg, mask);
  }

  // MOVNTDQA - 非临时对齐双字加载
  private executeMovntdqa(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.rm;

    // 计算内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 从内存加载128位数据（非临时加载，绕过缓存）
    const data = this.memoryManager.read(address, XMM_REGISTER_SIZE);

    // 写入目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, data);
  }

  // MOVNTI - 非临时四字存储
  private executeMovnti(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 源操作数是寄存器
    const srcReg = instruction.modrm.reg;
    const srcValue = this.registerManager.getRegister(srcReg);

    // 计算目标内存地址
    const address = this.calculateEffectiveAddress(instruction);

    // 执行非临时存储（绕过缓存）
    this.writeQwordToMemory(address, srcValue);
  }

  // PABSB - 计算打包字节的绝对值
  private executePabsb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = new Uint8Array(XMM_REGISTER_SIZE);

    // 对每个字节计算绝对值
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      // 将字节视为有符号数
      const signedByte = srcData[i] > 127 ? srcData[i] - 256 : srcData[i];
      // 取绝对值
      destData[i] = Math.abs(signedByte);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, destData);
  }

  // PABSD - 计算打包双字的绝对值
  private executePabsd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = new Uint8Array(XMM_REGISTER_SIZE);
    const view = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer);

    // 对每个双字计算绝对值
    for (let i = 0; i < XMM_REGISTER_SIZE / 4; i++) {
      // 读取有符号双字
      const value = view.getInt32(i * 4, true);
      // 取绝对值
      const absValue = Math.abs(value);
      // 写入结果
      destView.setInt32(i * 4, absValue, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, destData);
  }

  // PABSW - 计算打包字的绝对值
  private executePabsw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = new Uint8Array(XMM_REGISTER_SIZE);
    const view = new DataView(srcData.buffer, srcData.byteOffset);
    const destView = new DataView(destData.buffer);

    // 对每个字计算绝对值
    for (let i = 0; i < XMM_REGISTER_SIZE / 2; i++) {
      // 读取有符号字
      const value = view.getInt16(i * 2, true);
      // 取绝对值
      const absValue = Math.abs(value);
      // 写入结果
      destView.setInt16(i * 2, absValue, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, destData);
  }

  // PACKSSDW - 打包带符号饱和的双字到字
  private executePackssdw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 打包带符号饱和的双字到字
    for (let i = 0; i < 4; i++) {
      // 从目标和源XMM寄存器读取双字
      const destValue = destView.getInt32(i * 4, true);
      const srcValue = srcView.getInt32(i * 4, true);

      // 应用带符号饱和并转换为16位
      const saturatedDest = Math.max(-32768, Math.min(32767, destValue));
      const saturatedSrc = Math.max(-32768, Math.min(32767, srcValue));

      // 写入结果（目标值在偶数位置，源值在奇数位置）
      resultView.setInt16(i * 4, saturatedDest, true);
      resultView.setInt16(i * 4 + 2, saturatedSrc, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PACKSSWB - 打包带符号饱和的字到字节
  private executePacksswb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 打包带符号饱和的字到字节
    for (let i = 0; i < 8; i++) {
      // 从目标和源XMM寄存器读取字
      const destValue = destView.getInt16(i * 2, true);
      const srcValue = srcView.getInt16(i * 2, true);

      // 应用带符号饱和并转换为8位
      const saturatedDest = Math.max(-128, Math.min(127, destValue));
      const saturatedSrc = Math.max(-128, Math.min(127, srcValue));

      // 写入结果（目标值在偶数位置，源值在奇数位置）
      resultView.setInt8(i * 2, saturatedDest);
      resultView.setInt8(i * 2 + 1, saturatedSrc);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PACKUSWB - 打包无符号饱和的字到字节
  private executePackuswb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 打包无符号饱和的字到字节
    for (let i = 0; i < 8; i++) {
      // 从目标和源XMM寄存器读取字
      const destValue = destView.getInt16(i * 2, true);
      const srcValue = srcView.getInt16(i * 2, true);

      // 应用无符号饱和并转换为8位
      const saturatedDest = Math.max(0, Math.min(255, destValue));
      const saturatedSrc = Math.max(0, Math.min(255, srcValue));

      // 写入结果（目标值在偶数位置，源值在奇数位置）
      resultView.setUint8(i * 2, saturatedDest);
      resultView.setUint8(i * 2 + 1, saturatedSrc);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PALIGNR - 对齐向量元素
  private executePalignr(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取立即数（指定要向左移动的字节数）
    const shiftCount = Number(instruction.immediate & 0xFFn);

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 创建组合的源数据（目标+源）
    const combinedData = new Uint8Array(XMM_REGISTER_SIZE * 2);
    combinedData.set(destData, 0);
    combinedData.set(srcData, XMM_REGISTER_SIZE);

    // 执行对齐操作
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      resultData[i] = combinedData[shiftCount + i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PCLMULQDQ - 多项式乘法
  private executePclmulqdq(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取立即数（指定要使用的操作数部分）
    const imm = Number(instruction.immediate & 0x3n);

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 选择要相乘的操作数部分
    let a: bigint, b: bigint;
    if (imm === 0) {
      // 低64位 × 低64位
      a = BigInt(destView.getBigUint64(0, true));
      b = BigInt(srcView.getBigUint64(0, true));
    } else if (imm === 1) {
      // 低64位 × 高64位
      a = BigInt(destView.getBigUint64(0, true));
      b = BigInt(srcView.getBigUint64(8, true));
    } else if (imm === 2) {
      // 高64位 × 低64位
      a = BigInt(destView.getBigUint64(8, true));
      b = BigInt(srcView.getBigUint64(0, true));
    } else {
      // 高64位 × 高64位
      a = BigInt(destView.getBigUint64(8, true));
      b = BigInt(srcView.getBigUint64(8, true));
    }

    // 执行多项式乘法（简化版本）
    // 注意：实际的PCLMULQDQ执行的是模x^64 + 1的多项式乘法
    // 这里我们只实现简化版本
    const product = a * b;

    // 将结果写入目标XMM寄存器
    resultView.setBigUint64(0, product & 0xFFFFFFFFFFFFFFFFn, true);
    resultView.setBigUint64(8, product >> 64n, true);

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PCMPEQB - 打包字节比较
  private executePcmpeqb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 比较每个字节
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      // 如果相等，设置为全1；否则设置为全0
      resultData[i] = (destData[i] === srcData[i]) ? 0xFF : 0x00;
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // MOVQ - 四字移动 (XMM寄存器到XMM寄存器或内存)
  private executeMovq(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    if (instruction.modrm.mod === 3) {
      // 从XMM寄存器到XMM寄存器
      const destXmmReg = instruction.modrm.reg;
      const srcXmmReg = instruction.modrm.rm;

      // 读取源XMM寄存器的低64位
      const srcData = this.registerManager.getXmmRegister(srcXmmReg);
      const destData = new Uint8Array(XMM_REGISTER_SIZE);

      // 复制低64位（8字节）
      for (let i = 0; i < 8; i++) {
        destData[i] = srcData[i];
      }

      // 写入目标XMM寄存器
      this.registerManager.setXmmRegister(destXmmReg, destData);
    } else {
      // 从XMM寄存器到内存或从内存到XMM寄存器
      // 在SSE指令中，0F6F是从XMM到内存，而从内存到XMM需要另一个操作码
      // 这里假设当前指令是XMM寄存器到内存的形式
      const srcXmmReg = instruction.modrm.reg;
      const srcData = this.registerManager.getXmmRegister(srcXmmReg);
      const address = this.calculateEffectiveAddress(instruction);

      // 写入内存低64位
      for (let i = 0; i < 8; i++) {
        this.writeByteToMemory(address + BigInt(i), srcData[i]);
      }
    }
  }

  // PCMPEQD - 打包双字比较
  private executePcmpeqd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 比较每个双字
    for (let i = 0; i < XMM_REGISTER_SIZE / 4; i++) {
      const destValue = destView.getInt32(i * 4, true);
      const srcValue = srcView.getInt32(i * 4, true);

      // 如果相等，设置为全1；否则设置为全0
      const maskValue = (destValue === srcValue) ? 0xFFFFFFFF : 0x00000000;
      resultView.setInt32(i * 4, maskValue, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PCMPEQW - 打包字比较
  private executePcmpeqw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 比较每个字
    for (let i = 0; i < XMM_REGISTER_SIZE / 2; i++) {
      const destValue = destView.getInt16(i * 2, true);
      const srcValue = srcView.getInt16(i * 2, true);

      // 如果相等，设置为全1；否则设置为全0
      const maskValue = (destValue === srcValue) ? 0xFFFF : 0x0000;
      resultView.setInt16(i * 2, maskValue, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PCMPGTB - 打包字节大于比较
  private executePcmpgtb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 比较每个字节（视为有符号数）
    for (let i = 0; i < XMM_REGISTER_SIZE; i++) {
      // 将字节视为有符号数
      const destByte = destData[i] > 127 ? destData[i] - 256 : destData[i];
      const srcByte = srcData[i] > 127 ? srcData[i] - 256 : srcData[i];

      // 如果目标大于源，设置为全1；否则设置为全0
      resultData[i] = (destByte > srcByte) ? 0xFF : 0x00;
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PCMPGTD - 打包双字大于比较
  private executePcmpgtd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 比较每个双字（视为有符号数）
    for (let i = 0; i < XMM_REGISTER_SIZE / 4; i++) {
      const destValue = destView.getInt32(i * 4, true);
      const srcValue = srcView.getInt32(i * 4, true);

      // 如果目标大于源，设置为全1；否则设置为全0
      const maskValue = (destValue > srcValue) ? 0xFFFFFFFF : 0x00000000;
      resultView.setInt32(i * 4, maskValue, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PCMPGTW - 打包字大于比较
  private executePcmpgtw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 比较每个字（视为有符号数）
    for (let i = 0; i < XMM_REGISTER_SIZE / 2; i++) {
      const destValue = destView.getInt16(i * 2, true);
      const srcValue = srcView.getInt16(i * 2, true);

      // 如果目标大于源，设置为全1；否则设置为全0
      const maskValue = (destValue > srcValue) ? 0xFFFF : 0x0000;
      resultView.setInt16(i * 2, maskValue, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PEXTRW - 提取字
  private executePextrw(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取立即数（指定要提取的字索引）
    const index = Number(instruction.immediate & 0x7n);

    // 目标操作数是通用寄存器
    const destReg = instruction.modrm.reg;
    // 源操作数是XMM寄存器
    const srcXmmReg = instruction.modrm.rm;
    const srcData = this.registerManager.getXmmRegister(srcXmmReg);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);

    // 读取指定索引的字
    const value = srcView.getInt16(index * 2, true);

    // 写入目标通用寄存器（低16位）
    const destValue = this.registerManager.getRegister(destReg);
    const newValue = (destValue & 0xFFFFFFFFFFFFFF00n) | BigInt(value & 0xFFFF);
    this.registerManager.setRegister(destReg, newValue);
  }

  // PINSRW - 插入字
  private executePinsrw(instruction: DecodedInstruction): void {
    if (!instruction.modrm || instruction.immediate === undefined) return;

    // 获取立即数（指定要插入的位置索引）
    const index = Number(instruction.immediate & 0x7n);

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.rm;
    // 源操作数
    let srcValue: number;

    if (instruction.modrm.mod === 3) {
      // 源操作数是通用寄存器
      const srcReg = instruction.modrm.reg;
      srcValue = Number(this.registerManager.getRegister(srcReg) & 0xFFFFn);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcValue = Number(this.readWordFromMemory(address));
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const destView = new DataView(destData.buffer, destData.byteOffset);

    // 将源字插入到目标XMM寄存器的指定位置
    destView.setInt16(index * 2, Number(srcValue), true);

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, destData);
  }

  // PMULLD - 打包双字相乘
  private executePmulld(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);
    const destView = new DataView(destData.buffer, destData.byteOffset);
    const srcView = new DataView(srcData.buffer, srcData.byteOffset);
    const resultView = new DataView(resultData.buffer);

    // 对每个双字对执行乘法
    for (let i = 0; i < XMM_REGISTER_SIZE / 4; i++) {
      const destValue = destView.getInt32(i * 4, true);
      const srcValue = srcView.getInt32(i * 4, true);

      // 执行有符号双字乘法，保留低32位结果
      const product = destValue * srcValue;
      resultView.setInt32(i * 4, product, true);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // POPA - 弹出所有通用寄存器
  private executePopa(instruction: DecodedInstruction): void {
    // 按照特定顺序从栈中弹出寄存器：EDI, ESI, EBP, EBX, EDX, ECX, EAX
    // 在64位模式下，POPA指令被废弃，但我们仍然实现其基本功能

    // 获取当前栈指针
    let stackPointer = this.registerManager.getRegister(Registers.RSP);

    // 弹出EDI
    const ediValue = this.readQwordFromMemory(stackPointer);
    this.registerManager.setRegister(Registers.RDI, ediValue);
    stackPointer += 8n;

    // 弹出ESI
    const esiValue = this.readQwordFromMemory(stackPointer);
    this.registerManager.setRegister(Registers.RSI, esiValue);
    stackPointer += 8n;

    // 弹出EBP
    const ebpValue = this.readQwordFromMemory(stackPointer);
    this.registerManager.setRegister(Registers.RBP, ebpValue);
    stackPointer += 8n;

    // 弹出EBX
    const ebxValue = this.readQwordFromMemory(stackPointer);
    this.registerManager.setRegister(Registers.RBX, ebxValue);
    stackPointer += 8n;

    // 弹出EDX
    const edxValue = this.readQwordFromMemory(stackPointer);
    this.registerManager.setRegister(Registers.RDX, edxValue);
    stackPointer += 8n;

    // 弹出ECX
    const ecxValue = this.readQwordFromMemory(stackPointer);
    this.registerManager.setRegister(Registers.RCX, ecxValue);
    stackPointer += 8n;

    // 弹出EAX
    const eaxValue = this.readQwordFromMemory(stackPointer);
    this.registerManager.setRegister(Registers.RAX, eaxValue);
    stackPointer += 8n;

    // 更新栈指针
    this.registerManager.setRegister(Registers.RSP, stackPointer);
  }

  // MOVB - 字节移动
  private executeMovb(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数
    if (instruction.modrm.mod === 3) {
      // 目标是寄存器
      const destReg = instruction.modrm.reg;
      let srcValue: u64;

      if (instruction.modrm.rm === 0) {
        // 立即数到寄存器
        srcValue = instruction.immediate! & 0xFFn;
      } else {
        // 寄存器到寄存器 或 内存到寄存器
        if (instruction.opcode === 0x88) {
          // MOV r/m8, r8
          srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFn;
        } else {
          // MOV r8, r/m8
          const address = this.calculateEffectiveAddress(instruction);
          srcValue = BigInt(this.readByteFromMemory(address));
        }
      }

      // 写入目标寄存器的低8位
      const destValue = this.registerManager.getRegister(destReg);
      const newValue = (destValue & 0xFFFFFFFFFFFFFF00n) | srcValue;
      this.registerManager.setRegister(destReg, newValue);
    } else {
      // 目标是内存
      const address = this.calculateEffectiveAddress(instruction);
      let value: number;

      if (instruction.opcode === 0xC6 && instruction.modrm.reg === 0) {
        // MOV r/m8, imm8
        value = Number(instruction.immediate! & 0xFFn);
      } else {
        // 寄存器到内存
        value = Number(this.registerManager.getRegister(instruction.modrm.reg) & 0xFFn);
      }

      // 写入内存
      this.writeByteToMemory(address, value);
    }
  }

  // MOVL - 双字移动
  private executeMovl(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数
    if (instruction.modrm.mod === 3) {
      // 目标是寄存器
      const destReg = instruction.modrm.reg;
      let srcValue: u64;

      if (instruction.opcode === 0xB8 + destReg) {
        // MOV r32, imm32
        srcValue = instruction.immediate! & 0xFFFFFFFFn;
      } else {
        // 寄存器到寄存器 或 内存到寄存器
        if (instruction.opcode === 0x89) {
          // MOV r/m32, r32
          srcValue = this.registerManager.getRegister(instruction.modrm.rm) & 0xFFFFFFFFn;
        } else {
          // MOV r32, r/m32
          const address = this.calculateEffectiveAddress(instruction);
          srcValue = this.readDwordFromMemory(address);
        }
      }

      // 写入目标寄存器的低32位
      const destValue = this.registerManager.getRegister(destReg);
      const newValue = (destValue & 0xFFFFFFFF00000000n) | srcValue;
      this.registerManager.setRegister(destReg, newValue);
    } else {
      // 目标是内存
      const address = this.calculateEffectiveAddress(instruction);
      let value: u64;

      if (instruction.opcode === 0xC7 && instruction.modrm.reg === 0) {
        // MOV r/m32, imm32
        value = instruction.immediate! & 0xFFFFFFFFn;
      } else {
        // 寄存器到内存
        value = this.registerManager.getRegister(instruction.modrm.reg) & 0xFFFFFFFFn;
      }

      // 写入内存
      this.writeDwordToMemory(address, value);
    }
  }

  // PUNPCKHBW - 高位字节解包
  private executePunpckhbw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包高位字节
    for (let i = 0; i < 8; i++) {
      // 目标寄存器的高位字节
      resultData[i * 2] = destData[i + 8];
      // 源寄存器的高位字节
      resultData[i * 2 + 1] = srcData[i + 8];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUNPCKHDQ - 高位双字解包
  private executePunpckhdq(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包高位双字
    for (let i = 0; i < 4; i++) {
      // 目标寄存器的高位双字
      resultData[i * 4] = destData[i + 8];
      resultData[i * 4 + 1] = destData[i + 9];
      resultData[i * 4 + 2] = destData[i + 10];
      resultData[i * 4 + 3] = destData[i + 11];
      // 源寄存器的高位双字
      resultData[i * 4 + 8] = srcData[i + 8];
      resultData[i * 4 + 9] = srcData[i + 9];
      resultData[i * 4 + 10] = srcData[i + 10];
      resultData[i * 4 + 11] = srcData[i + 11];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUNPCKHQDQ - 高位四字解包
  private executePunpckhqdq(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包高位四字（移动整个高64位）
    for (let i = 0; i < 8; i++) {
      // 目标寄存器的高位四字
      resultData[i] = destData[i + 8];
      // 源寄存器的高位四字
      resultData[i + 8] = srcData[i + 8];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUNPCKHWD - 高位字解包
  private executePunpckhwd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包高位字
    for (let i = 0; i < 4; i++) {
      // 目标寄存器的高位字
      resultData[i * 4] = destData[i * 2 + 8];
      resultData[i * 4 + 1] = destData[i * 2 + 9];
      // 源寄存器的高位字
      resultData[i * 4 + 2] = srcData[i * 2 + 8];
      resultData[i * 4 + 3] = srcData[i * 2 + 9];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUNPCKLBW - 低位字节解包
  private executePunpcklbw(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包低位字节
    for (let i = 0; i < 8; i++) {
      // 目标寄存器的低位字节
      resultData[i * 2] = destData[i];
      // 源寄存器的低位字节
      resultData[i * 2 + 1] = srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUNPCKLDQ - 低位双字解包
  private executePunpckldq(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包低位双字
    for (let i = 0; i < 4; i++) {
      // 目标寄存器的低位双字
      resultData[i * 4] = destData[i];
      resultData[i * 4 + 1] = destData[i + 1];
      resultData[i * 4 + 2] = destData[i + 2];
      resultData[i * 4 + 3] = destData[i + 3];
      // 源寄存器的低位双字
      resultData[i * 4 + 8] = srcData[i];
      resultData[i * 4 + 9] = srcData[i + 1];
      resultData[i * 4 + 10] = srcData[i + 2];
      resultData[i * 4 + 11] = srcData[i + 3];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUNPCKLQDQ - 低位四字解包
  private executePunpcklqdq(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包低位四字（移动整个低64位）
    for (let i = 0; i < 8; i++) {
      // 目标寄存器的低位四字
      resultData[i] = destData[i];
      // 源寄存器的低位四字
      resultData[i + 8] = srcData[i];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUNPCKLWD - 低位字解包
  private executePunpcklwd(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 解包低位字
    for (let i = 0; i < 4; i++) {
      // 目标寄存器的低位字
      resultData[i * 4] = destData[i * 2];
      resultData[i * 4 + 1] = destData[i * 2 + 1];
      // 源寄存器的低位字
      resultData[i * 4 + 2] = srcData[i * 2];
      resultData[i * 4 + 3] = srcData[i * 2 + 1];
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // PUSHA - 压入所有通用寄存器
  private executePusha(instruction: DecodedInstruction): void {
    // 按照特定顺序压入寄存器：EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
    // 在64位模式下，PUSHA指令被废弃，但我们仍然实现其基本功能

    // 首先压入EAX
    this.executePushRegister(0); // RAX
    // 压入ECX
    this.executePushRegister(1); // RCX
    // 压入EDX
    this.executePushRegister(2); // RDX
    // 压入EBX
    this.executePushRegister(3); // RBX
    // 保存当前ESP（在压入之前的值）
    const originalEsp = this.registerManager.getRegister(Registers.RSP);
    // 压入ESP（这是压入之前的ESP值）
    this.push(originalEsp);
    // 压入EBP
    this.executePushRegister(5); // RBP
    // 压入ESI
    this.executePushRegister(6); // RSI
    // 压入EDI
    this.executePushRegister(7); // RDI
  }

  // RCPPS - 计算单精度浮点数的倒数
  private executeRcpps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 对每个单精度浮点数计算倒数
    for (let i = 0; i < 4; i++) {
      // 读取单精度浮点数
      const value = this.readFloatFromUint8Array(srcData, i * 4);
      // 计算倒数
      const reciprocal = 1.0 / value;
      // 写入结果
      this.writeFloatToUint8Array(resultData, i * 4, reciprocal);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  /**
   * 从Uint8Array中读取单精度浮点数（小端序）
   */
  private readFloatFromUint8Array(data: Uint8Array, offset: number): number {
    const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
    return view.getFloat32(offset, true); // true表示小端序
  }

  /**
   * 从Uint8Array中读取双精度浮点数（小端序）
   */
  private readDoubleFromUint8Array(data: Uint8Array, offset: number): number {
    const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
    return view.getFloat64(offset, true); // true表示小端序
  }

  /**
   * 将单精度浮点数写入Uint8Array（小端序）
   */
  private writeFloatToUint8Array(data: Uint8Array, offset: number, value: number): void {
    const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
    view.setFloat32(offset, value, true); // true表示小端序
  }

  /**
   * 将双精度浮点数写入Uint8Array（小端序）
   */
  private writeDoubleToUint8Array(data: Uint8Array, offset: number, value: number): void {
    const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
    view.setFloat64(offset, value, true); // true表示小端序
  }

  // UNPCKHPS - 高位单精度浮点数解包
  private executeUnpckhps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 组合源和目标数据
    const combinedData = new Uint8Array(XMM_REGISTER_SIZE * 2);
    combinedData.set(destData);
    combinedData.set(srcData, XMM_REGISTER_SIZE);

    // 提取高位单精度浮点数（每个XMM寄存器包含4个单精度浮点数）
    // 结果格式: dest[2], src[2], dest[3], src[3]
    for (let i = 0; i < 4; i++) {
      const srcIndex = i % 2 === 0 ? 2 + i : 2 + i - 1;
      const srcOffset = srcIndex * 4;
      const destOffset = i * 4;
      resultData.set(combinedData.slice(srcOffset, srcOffset + 4), destOffset);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // UNPCKLPS - 低位单精度浮点数解包
  private executeUnpcklps(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 目标操作数是XMM寄存器
    const destXmmReg = instruction.modrm.reg;
    // 源操作数
    let srcData: Uint8Array;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, XMM_REGISTER_SIZE);
    }

    // 获取目标XMM寄存器的值
    const destData = this.registerManager.getXmmRegister(destXmmReg);
    const resultData = new Uint8Array(XMM_REGISTER_SIZE);

    // 组合源和目标数据
    const combinedData = new Uint8Array(XMM_REGISTER_SIZE * 2);
    combinedData.set(destData);
    combinedData.set(srcData, XMM_REGISTER_SIZE);

    // 提取低位单精度浮点数
    // 结果格式: dest[0], src[0], dest[1], src[1]
    for (let i = 0; i < 4; i++) {
      const srcIndex = i % 2 === 0 ? i : i - 1;
      const srcOffset = srcIndex * 4;
      const destOffset = i * 4;
      resultData.set(combinedData.slice(srcOffset, srcOffset + 4), destOffset);
    }

    // 写回结果到目标XMM寄存器
    this.registerManager.setXmmRegister(destXmmReg, resultData);
  }

  // UCOMISS - 单精度浮点数无符号比较
  private executeUcomiss(instruction: DecodedInstruction): void {
    if (!instruction.modrm) return;

    // 源操作数（第二个操作数）
    let srcData: Uint8Array;
    let srcFloat: number;

    if (instruction.modrm.mod === 3) {
      // 源操作数是XMM寄存器
      const srcXmmReg = instruction.modrm.rm;
      srcData = this.registerManager.getXmmRegister(srcXmmReg);
      srcFloat = this.readFloatFromUint8Array(srcData, 0);
    } else {
      // 源操作数是内存
      const address = this.calculateEffectiveAddress(instruction);
      srcData = this.memoryManager.read(address, 4); // 单精度浮点数是4字节
      const view = new DataView(srcData.buffer, srcData.byteOffset, srcData.byteLength);
      srcFloat = view.getFloat32(0, true); // 小端序
    }

    // 目标操作数（第一个操作数）总是XMM0寄存器
    const destData = this.registerManager.getXmmRegister(0);
    const destFloat = this.readFloatFromUint8Array(destData, 0);

    // 清除ZF、CF、PF标志
    this.registerManager.setFlag('ZF', false);
    this.registerManager.setFlag('CF', false);
    this.registerManager.setFlag('PF', false);

    // 执行比较
    if (isNaN(destFloat) || isNaN(srcFloat)) {
      // 如果任一操作数是NaN，设置ZF=1, PF=1, CF=1
      this.registerManager.setFlag('ZF', true);
      this.registerManager.setFlag('PF', true);
      this.registerManager.setFlag('CF', true);
    } else if (destFloat === srcFloat) {
      // 如果相等，设置ZF=1, CF=0, PF=0
      this.registerManager.setFlag('ZF', true);
    } else if (destFloat > srcFloat) {
      // 如果目标大于源，设置CF=0, ZF=0, PF=0
      // 这里不做任何操作，因为我们已经清除了这些标志
    } else {
      // 如果目标小于源，设置CF=1, ZF=0, PF=0
      this.registerManager.setFlag('CF', true);
    }
  }

  // WRMSR - 写入模型特定寄存器
  private executeWrmsr(): void {
    // 获取ECX的值，它包含要写入的MSR地址
    const msrAddress = this.registerManager.getRegister(Registers.RCX) & 0xFFFFFFFFn;
    // 获取EDX:EAX的值，它包含要写入的数据
    const eaxValue = this.registerManager.getRegister(Registers.RAX) & 0xFFFFFFFFn;
    const edxValue = this.registerManager.getRegister(Registers.RDX) & 0xFFFFFFFFn;

    // 模拟写入MSR
    console.log(`WRMSR: Writing 0x${(edxValue << 32n | eaxValue).toString(16)} to MSR 0x${msrAddress.toString(16)}`);

    // 在JavaScript中，我们可以将MSR值存储在一个对象中
    // 实际实现中可能需要更复杂的模拟
  }

}